Code Examples
  Home arrow Code Examples arrow Page 3 - 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 - Classification and the Code Snippet


    (Page 3 of 4 )

    According to the breadth-first search, we can categorize the edges. They are four kinds of edges: edges that will became part of the tree, edges pointing ahead, edges pointing back and cross-pointing edges. Their names already tell you what they try to describe; however, let us see how we can learn what they are during our search.

    Each edge will be categorized in one of these sections, and this will happen the first time the search observes the existence of the edge. So that we can use the information later on, I decided to add another property during this algorithm to our adjacent list: the type of the edge. I also made a few definitions, as you will see below:

    #define TREE_EDGE 1

    #define BACK_EDGE 2

    #define CROSS_EDGE 3

    #define AHEAD_EDGE 4

    struct ListIt

    {

    int value;

    int type;

    ListIt* p_next;

    };

    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.

    Ahead-pointing edges should be gray, and the distance from the root is greater than the currently-visited node; however, inside a breadth-first search, this kind of edge does not exist, otherwise we would use them to visit that node.

    As mentioned on the previous page, we can find the shortest route by the number of edges from the root to specific vertex by re-iterating, and eventually by counting the number of iterations, we can also tell the distance. Now for didactic reasons we will store this information in a distance array that will point the level where we are in the graph.

    Everything I've tried to explain up to this point, translated to a C code snippet, will look as follows:

    void BFS(int rootVertex)

    {

    int u;

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

    {

    ancient[i] = 0;

    color[i] = WHITE;

    distance[i] = INT_MAX;

    }

     

    // insert root item

    ancient[rootVertex] = 0;

    color[rootVertex] = GRAY;

    distance[rootVertex] = 0;

     

    vertex_queue = new ListIt;

    vertex_queue->p_next = NULL;

    vertex_queue->value = rootVertex;

     

     

    pListIt p, qE;

    p = NULL;

    qE = vertex_queue;

     

     

    while(vertex_queue)

    {

    // get first item

    u = vertex_queue->value;

    printf(" %d ", u);

     

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

    if( !p->type)

    if (color[p -> value] == WHITE)

    {

    // add item to the queue

    color[p->value] = GRAY; // gray nod

    distance[p->value] = distance[u] +1;

     

    ancient[p->value] = u;

    p->type = TREE_EDGE; // this is a tree edge

    qE->p_next = new ListIt;

    qE = qE->p_next;

    qE->p_next = NULL;

    qE->value = p->value;

    }

    else // so no tree edge something else

    if (color[p->value] == GRAY) {

    p->type = BACK_EDGE;

    }

    else

    if (color[p->value] == BLACK && distance[p->value] <= distance[u]) {

    p->type = CROSS_EDGE;

    }

     

     

    // remove item from queue

    p = vertex_queue;

    color[p->value] = BLACK;

    vertex_queue = vertex_queue->p_next;

    delete p;

     

    }

     

     

    }

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