Code Examples
  Home arrow Code Examples arrow Page 4 - 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 topological order


    (Page 4 of 4 )

    The topological sorting is all about constructing an order of the vertexes which will make sure that, by drawing the vertexes from left to right, all of the edges will be directed in the right direction. Now from this we can conclude that this kind of order is possible only out of a directed and circle-free graph.

    This order is in fact the inverse order of the DFS tree, or in other words the inverse order of how the vertexes become black. What assures that this will be correct ? Whenever we step forward from a node and mark it as visited fully (painted black), all of its children are already visited; otherwise, we would not leave that point.

    Therefore, if we insert it now inside the list, all of the vertexes that stand at the other end of the out-going edges are already visited, and are on the left side of this item in the DFS order. From this we conclude that, in the inverse of this order, they will be on the right side.

    In code snippet terms, we only need to add a couple of lines after we mark an item as black (fully visited). We use a global variable to count the number of already-inserted items into the order; nevertheless, we could also inverse the order after the search:

    // add to the topological order

    ++atPos;

    topological_order[n-atPos] = vertex;

    -->Image Courtesy of Kátai Zoltán- Introduction to Graphs<--

    The execution of the application will print for us similar result to the upper image:

    1 4 3 6 10 9 11 14 18 17 13 16 22 21 12 15 20 19 2 5 8 7

     

    For all this and something extra, I've provided a little C file you can study. Inside it you will find the solution for the determination of the strong connecting components, the base circle system and the articulation edges and vertexes as a bonus to what we've learned so far.

    If you do not understand any of this, do not worry, as we will look over those items in detail during the next couple of articles related to graphs. Feel free to explore them until next time, when I will be back for another lesson on graphs.

    Thank you for investing the time to read my article and taking the effort to improve yourself. I want to encourage you to rate my article accordingly and to post your ideas and questions related to your experience with graphs here on the blog, or over on our friendly forum running under the name of DevHardware. Until next time, remember to Live With Passion!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

    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 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek