mirror of
https://github.com/TheAlgorithms/C
synced 2024-11-22 05:21:49 +03:00
aef9d8d53f
* file names modified * files name modified * file names modified * updating DIRECTORY.md Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
104 lines
2.9 KiB
C
104 lines
2.9 KiB
C
#include <stdbool.h>
|
|
#include <stdio.h>
|
|
#include "Graph.h"
|
|
|
|
#define MAX_NODES 1000
|
|
|
|
int visited[MAX_NODES]; // array to store visiting order
|
|
// indexed by vertex 0..nV-1
|
|
|
|
bool dfsPathCheck(Graph g, int nV, Vertex v, Vertex dest)
|
|
{
|
|
Vertex w;
|
|
for (w = 0; w < nV; w++)
|
|
if (adjacent(g, v, w) && visited[w] == -1)
|
|
{
|
|
visited[w] = v;
|
|
if (w == dest)
|
|
return true;
|
|
else if (dfsPathCheck(g, nV, w, dest))
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool findPathDFS(Graph g, int nV, Vertex src, Vertex dest)
|
|
{
|
|
Vertex v;
|
|
for (v = 0; v < nV; v++) visited[v] = -1;
|
|
visited[src] = src;
|
|
return dfsPathCheck(g, nV, src, dest);
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
int V = 6;
|
|
Graph g = newGraph(V);
|
|
|
|
Edge e;
|
|
e.v = 0;
|
|
e.w = 1;
|
|
insertEdge(g, e);
|
|
e.v = 0;
|
|
e.w = 4;
|
|
insertEdge(g, e);
|
|
e.v = 0;
|
|
e.w = 5;
|
|
insertEdge(g, e);
|
|
e.v = 5;
|
|
e.w = 4;
|
|
insertEdge(g, e);
|
|
e.v = 4;
|
|
e.w = 2;
|
|
insertEdge(g, e);
|
|
e.v = 4;
|
|
e.w = 3;
|
|
insertEdge(g, e);
|
|
e.v = 5;
|
|
e.w = 3;
|
|
insertEdge(g, e);
|
|
e.v = 1;
|
|
e.w = 2;
|
|
insertEdge(g, e);
|
|
e.v = 3;
|
|
e.w = 2;
|
|
insertEdge(g, e);
|
|
|
|
int src = 0, dest = 5;
|
|
if (findPathDFS(g, V, src, dest))
|
|
{
|
|
Vertex v = dest;
|
|
while (v != src)
|
|
{
|
|
printf("%d - ", v);
|
|
v = visited[v];
|
|
}
|
|
printf("%d\n", src);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
// By
|
|
// .----------------. .----------------. .----------------.
|
|
// .-----------------. .----------------. .----------------.
|
|
// | .--------------. || .--------------. || .--------------. ||
|
|
// .--------------. | | .--------------. || .--------------. | | | _________ |
|
|
// || | _____ _____ | || | __ | || | ____ _____ | | | | ____ ____
|
|
// | || | ____ | | | | | _ _ | | || ||_ _||_ _|| || | / \
|
|
// | || ||_ \|_ _| | | | | |_ || _| | || | .' `. | | | | |_/ | |
|
|
// \_| | || | | | | | | || | / /\ \ | || | | \ | | | | | | |
|
|
// |__| | | || | / .--. \ | | | | | | | || | | ' ' | | || |
|
|
// / ____ \ | || | | |\ \| | | | | | | __ | | || | | | | | | |
|
|
// | | _| |_ | || | \ `--' / | || | _/ / \ \_ | || | _| |_\ |_
|
|
// | | | | _| | | |_ | || | \ `--' / | | | | |_____| | || | `.__.'
|
|
// | || ||____| |____|| || ||_____|\____| | | | | |____||____| | || | `.____.'
|
|
// | | | | | || | | || | | || | | | | |
|
|
// | || | | | | '--------------' || '--------------' ||
|
|
// '--------------' || '--------------' | | '--------------' || '--------------'
|
|
// |
|
|
// '----------------' '----------------' '----------------'
|
|
// '----------------' '----------------' '----------------'
|
|
|
|
// Email : z5261243@unsw.edu.au
|
|
// hhoanhtuann@gmail.com
|