Code Examples
  Home arrow Code Examples arrow Page 2 - Depth-First Search in Graphs
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
CODE EXAMPLES

Depth-First Search in Graphs
By: Gabor Bernat
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 5
    2009-05-12

    Table of Contents:
  • Depth-First Search in Graphs
  • The code snippet and classification
  • Putting it together
  • The topological order

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Depth-First Search in Graphs - The code snippet and classification


    (Page 2 of 4 )

    This method reminds us of recursive iteration throughout the graph. As opposed to the BFS, the DFS is harder to comprehend, but easier to write. Again, for the best performance, the right way to store the graph inside of memory is with the adjacency list. The search can be wrapped up in just the following few lines:

    void DFS(int vertex)

    {

    printf(" %d " , vertex);

    pListIt p;

    color[vertex] = GRAY; // visited

     

    for (p = adjacencyList[vertex]; p != NULL; p = p -> p_next)

    if (!color[p -> value])

    {

    DFS(p -> value);

    }

    color[vertex] = BLACK;

    }

     

    For those of you who skipped my article about the breadth first search, I need to explain that to better simulate the state of the vertex I made a definition that a vertex is white if it has not yet been visited, gray if it is currently being visited, and black if it has been visited. Therefore, all we need to do is call the search for a vertex; if we find a neighbor that has not yet been visited, call the DFS for that vertex also. Let the recursion handle the rest.

    You may observe that the items are visited in the FIFO (First in First Out) order that is simulated by a stack. Yet we do not need to write the stack data structure ourselves, as the recursion will also implement this feature. This is why it is a little simpler to write a DFS than a BFS, where we need to manually simulate the queue. Nevertheless, you may write a non-recursive DFS (implemented with loops) where you need to use the stack also.  

    The order in which we drop by the vertexes will build the DFS search of the graph. As for the categorization of the edges, first we should know what kind of edges exist, so let us look at a little citation from my article about the BFS:

    "A tree edge means that the edge will become a part of the search tree (we traveled ahead on it inside the graph or found another vertex through it). Back-pointing edges are the ones that will point to a node already black (it points to an earlier generation vertex). Cross-pointing edges are gray, and their distance from the root is less than the distance from the root of the currently visited node."

    The DFS puts each edge in one of these four categories when it first observes its existence during the search. As we visit only to the edges that we have not yet visited (remember, visited edges are black), it is logical that our search will build a tree. This has all the edges through which we found another vertex. Additionally, we can classify the other edges as I explain in the next paragraph.

    Let us be at a vertex u. Moreover, we visit all of its neighbors, which we mark here with letter v. If v is white, the edge is a tree edge; if it is gray, it will be a back-pointing edge. If it is black, there are two cases. We measure the distance of the vertexes from the root, and if the distance of vertex v is larger than of vertex u we have an ahead-pointing edge, otherwise it will be a cross-pointing edge.

    More Code Examples Articles
    More By Gabor Bernat


     

    CODE EXAMPLES ARTICLES

    - Bipartite Graphs
    - Connectivity in Graphs
    - The Ford-Fulkerson Algorithm
    - Critical Paths
    - The Bellman-Ford and Roy-Floyd Algorithms
    - Shortest Path Algorithms in Graphs
    - Minimum Spanning Tree
    - Articulation Edges and Vertexes
    - Circles and Connectivity in Graphs
    - Depth-First Search in Graphs
    - Breadth-First Search in Graphs
    - The Prufer Code and the Floyd-Warshall Algor...
    - An Insight into Graphs
    - Coding a Custom Object with WSC
    - Creating a Custom Object with WSC





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek