473,396 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

depth first search

I have a depth first search algorithm like below

Mark all nodes unvisited.
While nodes are unvisited, perform DFS on them.
Function DFS (node aNode)
...Visit (aNode)
...Set aNode to be visited
...For each neighbor N of aNode that is unvisited, DFS(N)


can anybody help me convert it into C source code
Nov 30 '07 #1
6 21004
Ganon11
3,652 Expert 2GB
Which part are you having trouble with? Do you have your Graph set up, with Nodes and Edges?
Nov 30 '07 #2
yeah, some kind of homework. I've just done a bit:

Expand|Select|Wrap|Line Numbers
  1. /* assume there are n nodes in the graph */
  2. void main() 
  3. {
  4.     int i;
  5.     for (i=1;i<=n;i++)
  6.     {
  7.         node[i]=0;             // 0 means unvisited
  8.     }                        // mark all nodes in the graph                             // as unvisited
  9.     for(i=1;i<=n;i++)
  10.     {
  11.         while(node[i]==0)        //while a node is unvisited
  12.         {                    //call function dfs()
  13.             dfs(node[i]);
  14.         }
  15.     }
  16.  
  17.     getch();
  18. }
  19.  
  20. void dfs(node n)                // define function dfs()
  21. {
  22.     node w;
  23.     n=1;                     // 1 means visited
  24.     for each w in adj_list(n)    // adj_list(n) shows all nodes         {                    // that are connected to n
  25.             If(w==0)
  26.             {
  27.                 dfs(w)        //call dsf()function recursively
  28.             }
  29.         }    
  30. }
i'm quite sure the function dfs() is not correct. can u help me to correct it or, better, the entitre source code

thanks!
Nov 30 '07 #3
yeah, some kind of homework. I've just done a bit:
Expand|Select|Wrap|Line Numbers
  1. /* assume there are n nodes in the graph */
  2. void main() 
  3. {
  4. int i;
  5.     for (i=1;i<=n;i++)
  6.     {
  7.         node[i]=0;                
  8.     }                                                                                   
  9.     for(i=1;i<=n;i++)  <------ HERE IS THE PROBLEM 
  10.     {
  11.         while(node[i]==0)        //while a node is unvisited
  12.         {                    //call function dfs()
  13.             dfs(node[i]);
  14.         }
  15.     }
  16.     getch();
  17. }
  18. void dfs(node n)            // DFS functions it's correct    
  19. {
  20.     node w;
  21.     n=1;                     // 1 means visited
  22.     for each w in adj_list(n)            {
  23.             If(w==0)
  24.             {
  25.                 dfs(w)        //call dsf()function recursively
  26.             }
  27.         }    
  28. }
  29.  
thanks!
instead of calling the dfs functions on every node,as you do in the for loop in main function,you 'd rather choose a starting node,call dfs function just on that node and then wait for the magic of recursion to do all the work.Hope you understood,otherwise send me a PM.
Nov 30 '07 #4
yeah, I see the case: the for loop actually can run once only because the dfs() function inside it marks all the nodes as visited. And the problem is, the for loop makes the starting node always the first one, right? so how can I randomly choose starting node in C?
Nov 30 '07 #5
I can't asnwer because I don't know how you stored the nodes.
Nov 30 '07 #6
I can't asnwer because I don't know how you stored the nodes.
Assume that nodes are store in 2-dimensional array, so how it would be?
- to choose a random starting node
- to make a list of its neighbour, and put on them a for loop

i really get stuck at this, so can you help me?
Dec 1 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: News | last post by:
I am new in using Python Anyone know how to implement breadth first search using Python? Can Python create list dynamically, I want to implement a program which will read data from a file and...
1
by: howa | last post by:
Hi, Is that for binary search tree, depth first search = in order search? sorry as i can't find any suitable newsgroup to ask... thanks.
3
by: alaa123 | last post by:
hi plz do not ignore me I really neeed ur help I am working on a progarmme to implement First Search and Breadth Search I did the first search algorithm but I need help with implementing the...
4
by: SantaClaus | last post by:
Hi all, I'm implementing a depth first search algorithm to determine strong components. If anybody has simple algorithm or sample code, please post it. Thanks in advance.
3
by: majidmajid | last post by:
hi.... i need code for defth first search alogithm.
1
by: Lord Raiden | last post by:
Hello lads i have a problem about the Breadth First Search . I need to do an implementation of a checking algorith that check from a Game Graph that i have create if every non-object node appears...
0
by: coosa | last post by:
Dear all; My code is is a bit long but is modular at least. I'm attempting to implement the depth first search for an application that is supposed to: 1- Fetch based on an ID from the database a...
2
by: maklacuz | last post by:
Hi, everyone is there anyone who can heip me implement a code to solve the map coloring problem using the depth-first search in C++ ? any help is welcome.
2
by: Man4ish | last post by:
Hi , How we can find the cyclic path and path between two nodes using Depth first search algorithm in Graph Data Structure. Can i get Pseudo code or code doing the same.I will be very much thankful...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.