Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is an implementation of DFS Search in C. Can be modified to work with MultiGraphs imo.</p> <p>If this is what you are looking for, I can modify it to work with 'MultiGraphs'..</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;malloc.h&gt; #include &lt;conio.h&gt; typedef struct AdjacencyList { char VertexID; int *IncidenceList; }AdjList; typedef struct Graph { int VertexCount, Status; char VertexName; AdjList List; struct Graph *Next; }Graph; Graph* InitializeGraph(); Graph *InitGraphNode(int cnt); void PerformDepthFirstSearch(Graph *graph); int main() { Graph *graph; graph = InitializeGraph(); PerformDepthFirstSearch(graph); return 0; } Graph *InitGraphNode(int cnt) { Graph *node = ((Graph*)malloc(sizeof(Graph))); node-&gt;List.IncidenceList = ((int*)malloc(sizeof(int)*cnt)); node-&gt;Next = NULL; return node; } Graph* InitializeGraph() { Graph *graphHead = NULL, *node, *tmp, *tmp2; char vname; int cnt, i, j, isIncident = 0; printf("Enter the number of vertices : "); scanf("%d", &amp;cnt); if (cnt == 0) { printf("Number of vertices cannot be ZERO!!\n\n"); return graphHead; } graphHead = InitGraphNode(1); graphHead-&gt;VertexCount = cnt; for(i = 0; i &lt; cnt; i++) { printf("VertexName : "); vname = getche(); printf("\n"); node = InitGraphNode(cnt); node-&gt;VertexName = vname; node-&gt;Next = NULL; node-&gt;Status = 1; if (graphHead-&gt;Next == NULL) { graphHead-&gt;Next = node; } else { tmp = graphHead; while (tmp-&gt;Next != NULL) { tmp = tmp-&gt;Next; } tmp-&gt;Next = node; } } tmp = graphHead-&gt;Next; printf("Prepare to input the adjacent elements of corresponding vertices...\n\n"); for(i = 0; i &lt; cnt; i++) { vname = tmp-&gt;VertexName; tmp2 = graphHead-&gt;Next; for(j = 0; j &lt; cnt; j++) { here : printf("%c incident on %c :: (1 for YES)(0 for NO) ::-&gt; ",vname, tmp2-&gt;VertexName); scanf("%d", &amp;isIncident); if ((isIncident &lt; 0)||(isIncident &gt; 1)) { printf("Wrong Input!! Try again!!\n\n"); goto here; } tmp-&gt;List.IncidenceList[j] = isIncident; tmp2 = tmp2-&gt;Next; } tmp = tmp-&gt;Next; } return graphHead; } void PerformDepthFirstSearch(Graph *graph) { Graph *Stack[100], *Copy = graph-&gt;Next, *node, *tmp = graph-&gt;Next; int top = 0, i, cnt = 0; Stack[top++] = Copy; Copy-&gt;Status++; while(top != 0) { node = Stack[--top]; node-&gt;Status++; printf("%c, ", node-&gt;VertexName); for(i = 0; i &lt; graph-&gt;VertexCount; i++) { if(node-&gt;List.IncidenceList[i] == 1) { while (cnt &lt; i) { tmp = tmp-&gt;Next; cnt++; } if (tmp-&gt;Status == 1) { Stack[top++] = tmp; tmp-&gt;Status++; } } } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload