Home arrow Development Cycles arrow Page 2 - Learning About the Graph Construct using Games, Part II
DEVELOPMENT CYCLES

Learning About the Graph Construct using Games, Part II


In this second article in a three part series, we learn about two famous algorithms that can be used to solve the classic shortest path problem, and finally get the solution to the water jug division task set in the first part (no fair reading ahead to find it!).

Author Info:
By: Mohamed Saad
Rating: 5 stars5 stars5 stars5 stars5 stars / 33
March 15, 2005
TABLE OF CONTENTS:
  1. · Learning About the Graph Construct using Games, Part II
  2. · Dijkstra's algorithm
  3. · Algorithm in action
  4. · Floyd-Warshall algorithm
  5. · Back to the water problem

print this article
SEARCH DEVARTICLES

Learning About the Graph Construct using Games, Part II - Dijkstra's algorithm
(Page 2 of 5 )

Okay, here is the deal: we have a graph; the edges are weighted. You are given a node, and you want to find the shortest path from this node to all other nodes. The path length is the sum of weights of all edges on this path. (If edges are not weighted, you can assume that every edge has a weight of one, and in this case, the shortest path is the one with the fewest number of edges on it).

How can you solve this problem? I encourage you to think about it for a second. How many ideas would you come up with? How many of them actually work?

Anyway, here is the idea Dijkstra came up with…

Preparation step:

Dijkstra's algorithm uses three additional arrays. The three arrays have a number of elements equal to the number of nodes in the graph.

The first array (let's call it isVisited[]) is a boolean array, and it keeps track of which nodes have been reached.

The second array (let's call it distance[]) is an integer array, and it keeps track of the minimum distances from the start node to every node in the graph.

At the beginning, the isVisited array starts with all false, except for the entry of the start node, which is set to true.

The distance array initially starts with

    1. a zero in the entry of the start node (This is the distance from the starting node to itself).
    2. infinity for any node that you can't reach immediately from the start node (a very big integer will do).
    3. the edge weight for any node that you can reach immediately from the start node.

The third array is called the previous[]. For every node, this array holds the node immediately previous to it in the shortest path. The initial value for this array the index of the start node.

For example, for this graph, if we are trying to find shortest paths starting at node A…

 

The three arrays will start like this…

The only interesting one is the distance array. The first entry is 0 since a is the initial node itself. The last two entries are 5 and 12 since a is connected to both e and f, and the edges have these weights. a is not connected to any other nodes, so they are all set to .

Algorithm steps:

  1. Select the node with the smallest distance (in the distance array), that has not been visited before (isVisited is false). Let's call this node X.
  2. Set its visited status to true.
  3. For every node Y that has not been visited yet, see if it is better to go from the start node to X, and then from X to Y. If it is better than the current entry in the distance array, update the distance array, and make sure that the previous of Y is set to X.
  4. If there are still more unvisited nodes, go back to 1

See, it is a piece of cake! What? It is not? Okay, let me explain.

Basically, Dijkstra's algorithm divides the nodes into two sets. These are the nodes that we have already reached (we now know the shortest path to), and the nodes we have not yet reached (we don't know the shortest path to). Initially, the only node we know the path to is the initial node (we don't even have to move to reach it, do we?); all other nodes are still in the other set.

With every step, we select the node that is nearest to the initial node. Since it is the nearest to the initial node, the path to it is the shortest path to this node. We mark it as visited, and then we do a very important step: for all the remaining unvisited nodes, we check to see if going from the starting node to the newly visited node, and then to this node, makes any improvements. If it does, we update the distance array for the corresponding node.


blog comments powered by Disqus
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

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials