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

Breadth-First Search in Graphs
By: Gabor Bernat
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 2
    2009-05-11

    Table of Contents:
  • Breadth-First Search in Graphs
  • Breadth-first approach in more detail
  • Classification and the Code Snippet
  • Inevitable Vertexes

  • 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


    Breadth-First Search in Graphs - Inevitable Vertexes


    (Page 4 of 4 )

    This is one way to use the breadth-first search. This tries to show that with just a little adjustment to our search, we can solve problems that at first may seem quite difficult. The issue is as follows: let there be a graph that has a source vertex (these have only out-going edges) and a drain vertex (only in-coming edges). Determine the vertexes that are inevitable; in other words, all the roads from the source to the drain pass through them.

    We need to observe that with a breadth-first search, as we step further from a gray vertex that has all of its incoming edges black, the nodes that at a point remain alone in the queue will be the inevitable points. This is a key point -- for example, in the road system of a country -- and we need to take special care of these vertexes to illustrate a practical usage of the algorithm.

    To further simplify the count of the incoming edges, we are going to execute the search at the inverse of the graph (so we invert the direction of all edges). This way we can check the outgoing edges, which is an easy task with the adjacent list. In addition, here is the modified search code snippet:

    void BFS_inevitable(int root)

    {

    int u;

    for (int i =1 ; i <= n ; ++i)

    if (i != root)

    {

    color[i] = WHITE;

    }

    color[root] = GRAY;

     

    vertex_queue = new ListIt;

    vertex_queue->p_next = NULL;

    vertex_queue->value = root;

     

    pListIt p, qE, q;

    q = NULL;

    p = NULL;

    qE = vertex_queue;

     

     

    while(vertex_queue)

    {

     

    if (!vertex_queue->p_next) // only one item

    {

    printf(" %d ", vertex_queue->value);

    }

     

    // we get the point for which all neighbors are

    //already visited

     

    u = 0; // invalid point

    for (p = vertex_queue; p != NULL && !u;

    p = p -> p_next) // until we find a valid point

    {

    // check if all the edges towards it point is touched

    for (q = inverseList[p->value]; q != NULL;

    q = q -> p_next) // until we find a valid point

    if( !color[q->value])

    break;

    if(!q)

    u = p->value;

    }

     

     

    for (p = v[u]; p != NULL; p = p -> p_next)

    if (!color[p -> value])

    {

    //add it to the queue

     

    color[p->value] = GRAY;

    qE->p_next = new ListIt;

    qE = qE->p_next;

    qE->p_next = NULL;

    qE->value = p->value;

    }

     

     

    // remove the item from the queue

    // mark as black

    p = vertex_queue;

    color[p->value] = BLACK;

    vertex_queue = vertex_queue->p_next;

    delete p;

    }

     

     

    }

    You may download the C file for everything I presented here today and observe the full program, where you may enter new graphs to perform tests and see the code in action.

    This will be all for today, so I would like to thank you for reading all the way to the end, and ask you to rate my article and comment it here on the blog if you feel like you want to say something about it. As an alternative, you may also join our friendly forum over at DevShed or DevHardware and act similarly there. I will be back next time with the depth-first search, consequently until then 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 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek