Development Cycles
  Home arrow Development Cycles arrow Page 5 - Learning About the Graph Construct using G...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
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? 
DEVELOPMENT CYCLES

Learning About the Graph Construct using Games, Part III
By: Mohamed Saad
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 7
    2005-03-22

    Table of Contents:
  • Learning About the Graph Construct using Games, Part III
  • Conditions for a Solution to Exist
  • Representing the Problem as a Graph
  • The Steps Required
  • Depth First Traversals
  • Solving the Problem

  • 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


    Learning About the Graph Construct using Games, Part III - Depth First Traversals


    (Page 5 of 6 )

    A traversal is like walking on the nodes of a graph. The purpose of a traversal is to visit all nodes of a graph. There are several traversal methods such as Breadth first traversal and Depth first traversal. We will concentrate on depth first traversal here, since it is simpler. Using traversals, we will be able to find paths and loops easily in the graph.

    A depth first traversal tries to go as deep as possible in the graph before visiting nodes near to the starting point. For example in a graph like this:

    If we start at node a, it will try to go as deep as possible, so a possible scenario could be… a->b->c->d->e->f->g->h (a depth first traversal is clearly not unique, we could have started with the path to c)

    Depth first traversal is extremely easy to code. For every node, we mark it as visited and recursively call the traversal for each of the unvisited neighboring nodes. (I hope you are familiar with the concept of recursion. If not, please check my article here: http://www.devshed.com/c/a/Practices/Solving-Problems-with-Recursion/)

    Here is the code for a simple depth first traversal:

    void depthFirstTraversal(int u)
    {
      System.out.println("Visiting node "+u);
      visited[u]=true;
      for(node2 w=((node)(data.elementAt(u))).links;w!=null;w=w.next)
      {
        if(!visited[w.id])
          depthFirstTraversal(w.id,v,color);
      }
    }

    We are assuming the same adjacency list representation from parts 1 and 2. I hope you haven't already forgotten about them. Each node has a linked list holding all the nodes to which it is connected.

    So, what does Depth First traversal have to do with path finding? A lot, actually! A slight modification of this function will allow us to find a path (or a loop) between two nodes in a graph.

    The idea is simple. To find a path from u to v, we will keep traversing the graph starting at u till we hit the node v. Before we visit any node, we mark the edge leading to it as taken. If no path is found through this path, we reset the state of the edge to not taken. When we stop after hitting v, the path leading from u to v will be marked as taken. Pretty simple, eh?

    Here is the modified function:

    boolean markPath(int u,int v,int color)
    {
      visited[u]=true;
      for(node2 w=((node)(data.elementAt(u))).links;w!=null;w=w.next)
      {
        if(w.color!=0)
          continue;
        markColor(u,w.id,color);
        if(w.id==v)
          return true;
        if(!visited[w.id])
          if(markPath(w.id,v,color))
            return true;
          markColor(u,w.id,0);
      }
      return false;
    }

    You may notice we have an extra parameter, called color. This is because, as we mentioned before, we are going to be finding a path, and several loops, so it is important to distinguish them. We mark the edge we take by a certain color, to indicate it belongs to a certain loop, or path.

    The second observation is the markColor function. What is it? Remember in part 1 when we talked about undirected graphs, and how they are represented by two edges between any two nodes instead of one. The two edges must be treated as one. The markColor function simply colors the two versions of the edge: the one going from u to v, and the one going from v to u.

    void markColor(int u,int v,int color)
    {//mark the 2 versions of the edge (u,v) with color
      for(node2 w=((node)(data.elementAt(u))).links;w!=null;w=w.next)
      {
        if(w.id==v)
        {
          w.color=color;
          break;
        }
      }
      for(node2 w=((node)(data.elementAt(v))).links;w!=null;w=w.next)
      {
        if(w.id==u)
        {
          w.color=color;
          break;
        }
      }
    }

    More Development Cycles Articles
    More By Mohamed Saad


     

    DEVELOPMENT CYCLES ARTICLES

    - Division of Large Numbers
    - Branch and Bound Algorithm Technique
    - Dynamic Programming Algorithm Technique
    - Genetic Algorithm Techniques
    - Greedy Strategy as an Algorithm Technique
    - Divide and Conquer Algorithm Technique
    - The Backtracking Algorithm Technique
    - More Pattern Matching Algorithms: B-M
    - Pattern Matching Algorithms Demystified: KMP
    - Coding Standards
    - A Peek into the Future: Transactional Memory
    - Learning About the Graph Construct using Gam...
    - Learning About the Graph Construct using Gam...
    - Learning About the Graph Construct using Gam...
    - How to Strike a Match







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