Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graphs – Part III CS 367 – Introduction to Data Structures.

Similar presentations


Presentation on theme: "Graphs – Part III CS 367 – Introduction to Data Structures."— Presentation transcript:

1 Graphs – Part III CS 367 – Introduction to Data Structures

2 Finding the Shortest Path Often desirable when to find the shortest path from one node in a graph to another –very common problem in networking To find this, must know the cost of going from one node to another –use a weighted graph were weights represent costs (or distances) –how do we record these weights?

3 Recording Weights The simplest solution is to use an adjacency matrix –instead of simply putting a 1 in a cell to indicate a connection, put the weight –the maximum integer value would be used to represent no connection an infinite cost –the cost for node to journey to itself is zero the diagonal of the matrix will be all zeroes

4 Recording Weights A B D C E F 3 2 4 3 8 4 5 2 A B C D E F BCDAEF 0 0 0 0 0 0 23∞ ∞34 ∞4 8∞∞ ∞ ∞5 ∞ ∞∞ ∞∞ ∞2 ∞∞ ∞∞∞∞ ∞ ∞ ∞

5 Storing Shortest Distance Info Each node will contain a list of the shortest distances from itself to every other node –a value equal to the maximum integer will mean no route exists Each node must also contain information about the first hop on its journey to another node –store this with the shortest distance info Will create a new class called DistanceTable

6 Distance Table Class class DistanceEntry { public int distance; public int firstHop; public DistanceEntry(int distance, int firstHop) { distance = Integer.MAX_VALUE; firstHop = -1; } class DistanceTable { public DistanceEntry[ ] routes; public DistanceTable(int numNodes) { routes = new DistanceEntry[numNodes]; for(int i=0; i<numNodes; i++) { routes[i] = new DistanceEntry(); } }

7 New Graph Node class GraphNode { public Object data; public boolean visited; public int index; public DistanceTable table; public GraphNode(Object data, int numNodes, int index) { this.data = data; this.index = index; visited = false; table = new DistanceTable(numNodes); table.routes[index].distance = 0; }

8 Theorem and Proof Theorem if there exists a route, R1, from A to B that costs x and there exists no other route from A to any other node that costs less than x, then R1 is the lowest cost route from A to B Proof route, R1, from A to B costs x any other route, R2, from A to C costs y (y ≥ x) a third route, R3, from C to B costs z any route, R4, from A to C to B will cost y + z (y ≥ x) so (y + z) must be greater than x

9 Dijkstra’s Algorithm Basic idea –place all possible destination nodes in a set can represent this with an array, linked list, or heap –give each entry a specific starting value neighbors have their weights as values all other nodes have infinity (or a large number) –remove the lowest value from the current set this is the shortest route to this node –add removed nodes neighbors to the set if they are not already there they have a lower value than the one currently in the set –repeat until set is empty or only infinite values remain

10 Dijkstra’s Algorithm Iteration 12345 B2---- C33--- D∞55-- E∞∞∞∞12 F∞10 7- A B D C E F 3 2 4 3 8 8 5 2 Distance Table for A Distance First Hop 235712 BCBBB B C D E F

11 Dijkstra’s Algorithm Obviously the previous example excluded one key piece of data –when updating the set, must track the first hop along with the distance Notice that the second time D was encountered (going through C) it was ignored –already had a smaller value going through B However, the second time F was encountered, its value was entered into the table (going through D) and the first one was discarded –shorter to go through D than directly from B

12 Pseudo-Code shortestPath-Dijkstra(int[ ][ ] matrix, GraphNode[ ] nodes, int start) { // for all vertices v set the current distance to Integer.Max_Value // current distance to the source equals 0 // create array of items to be checked (toBeChecked) // set all items to be checked (true in every element of toBeChecked) while(// toBeChecked still has a true value in it) { v = // a vertex in toBeChecked with smallest current distance // remove v from toBeChecked (set it to false) // for all vertices u adjacent to v and in toBeChecked if(current distance of u > current distance to v + weight(v to u)) { // current distance u = current distance to v + weight(v to u) // first hop to u equals the first hop to v }

13 Label Setting vs. Label Correcting Dijkstra’s algorithm is considered label setting –it sets a minimum distance and immediately removes it sets it for good Some algorithms are label correcting –they never remove a node from set to check –instead, they keep updating them until no more values can be changed

14 Bellman-Ford Algorithm Basic idea –place all possible destination nodes in a set –give each entry a specific starting value neighbors have their weights as values all other nodes have infinity (or a large number) –as long as there exists an edge that produces a lower distance than the current one change the current distance to the new one

15 Bellman-Ford Algorithm Iteration 123 B222 C333 D∞55 E∞∞12 F∞77 A B D C E F 3 2 4 3 8 8 5 2 Distance Table for A Distance First Hop 235712 BCBBB B C D E F

16 Bellman-Ford Algorithm shortestPath-Dijkstra(int[ ][ ] matrix, GraphNode[ ] nodes, int start) { // for all vertices v set the current distance to Integer.Max_Value // current distance to the source equals 0 while(// there is any node u where current distance to u greater than // current distance to another node v + weight(vu)) { // current distance to u = current distance to v + weight(vu) }

17 Ford vs. Dijkstra Dijkstra’s algorithm is simpler to implement –believe it or not Ford’s algorithm works even if there are negative weights in the graph –Dijkstra’s algorithm fails in this case


Download ppt "Graphs – Part III CS 367 – Introduction to Data Structures."

Similar presentations


Ads by Google