Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
March 26th, 2013, 10:52 AM
 fun girl
Registered User

Join Date: Mar 2013
Posts: 1
Time spent in forums: 4 m 11 sec
Reputation Power: 0
C program

Hi, guys
I really wanna understand how this code works

----------------
it's a program to implement depth first search

#include<stdio.h>
#define MAX 5

int dfs(int adj[][MAX], int visited[], int start)
{
int stack[MAX];
int top=-1,i;
printf("%c-",start+65);
visited[start]=1;
stack[++top]=start;

while(top!=-1)
{

start=stack[top];
for(i=0;i<MAX;i++)
{
{
stack[++top]=i;
printf("%c-",i+65);
visited[i]=1;
break;
}
}
if(i==MAX)
top--;
}
return 0;
}

int main()
{
int visited[MAX]={0};
printf("DFS Traversal : ");
printf("\n");
return 0;
}

I would be grateful if you helped me!!!

#2
March 26th, 2013, 12:23 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,213
Time spent in forums: 1 Month 3 Weeks 2 Days 17 h 59 m 58 sec
Reputation Power: 455
The adjacency matrix stores the graph.
Code:
To
a b c d e
+---------
a|0 0 1 1 0
f  b|0 0 0 0 0
r  c|0 1 0 1 1
o  d|0 0 0 0 1
m  e|0 0 0 1 0
where a 1 indicates a directed connection of nodes. The matrix isn't symmetric.

The stack holds the frontier---the list of the nodes accessible from nodes visited. But because of the break statement the stack gets only the next node on the frontier. And continues with the new start position. Eventually the stack pops back when the code can continue to look for additional nodes on the frontier of a previous start node.
__________________
[code]Code tags[/code] are essential for python code!

Last edited by b49P23TIvg : March 26th, 2013 at 01:13 PM.

#3
March 26th, 2013, 04:37 PM
 clifford
Contributing User

Join Date: Aug 2003
Location: UK
Posts: 4,975
Time spent in forums: 1 Month 4 Days 5 h 12 m 47 sec
Reputation Power: 1801
Quote:
 Originally Posted by fun girl I really wanna understand how this code works
The best way to do that is to single-step the code in your debugger.

You could learn the fundamentals of the debugger - stepping, watching, and breakpoints in about the time you spent posting this question and reading the answers, but the lesson will pay back many times over in code debugging, testing and comprehension. The return on investment will easily break-even the first time you use it on this code in that say it took you an hour to figure it out manually, with half an hour debugger familiarisation, it will then take you less than half an hour to achieve the same level of comprehension with the debugger, and next time you already know how to use the debugger.

 Viewing: Dev Shed Forums > Programming Languages > C Programming > C program