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

Slides:



Advertisements
Similar presentations
CS 206 Introduction to Computer Science II 04 / 01 / 2009 Instructor: Michael Eckmann.
Advertisements

Weighted graphs Example Consider the following graph, where nodes represent cities, and edges show if there is a direct flight between each pair of cities.
Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both.
Graph II MST, Shortest Path. Graph Terminology Node (vertex) Edge (arc) Directed graph, undirected graph Degree, in-degree, out-degree Subgraph Simple.
CS 206 Introduction to Computer Science II 11 / 10 / 2008 Instructor: Michael Eckmann.
Graphs. Graph definitions There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs Birmingham Rugby London Cambridge.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
Chapter 9 Graph algorithms Lec 21 Dec 1, Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
Shortest path algorithm. Introduction 4 The graphs we have seen so far have edges that are unweighted. 4 Many graph situations involve weighted edges.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Shortest Path Problem For weighted graphs it is often useful to find the shortest path between two vertices Here, the “shortest path” is the path that.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
More Graph Algorithms Weiss ch Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.
Dijkstra’s Algorithm Slide Courtesy: Uwash, UT 1.
1 Shortest Path Algorithms. 2 Routing Algorithms Shortest path routing What is a shortest path? –Minimum number of hops? –Minimum distance? There is a.
1 Shortest Path Calculations in Graphs Prof. S. M. Lee Department of Computer Science.
Dijkstra’s Algorithm. 2 Shortest-path Suppose we want to find the shortest path from node X to node Y It turns out that, in order to do this, we need.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms Shortest-Path.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
WAN technologies and routing Packet switches and store and forward Hierarchical addresses, routing and routing tables Routing table computation Example.
COSC 2007 Data Structures II Chapter 14 Graphs III.
Shortest Path Problem Weight of the graph –Nonnegative real number assigned to the edges connecting to vertices Weighted graphs –When a graph.
Dijkstra’s Algorithm Supervisor: Dr.Franek Ritu Kamboj
Shortest Path in Weighted Graph : Dijkstra’s Algorithm.
Graphs – Part II CS 367 – Introduction to Data Structures.
CS 206 Introduction to Computer Science II 11 / 16 / 2009 Instructor: Michael Eckmann.
Decision Maths 1 Shortest path algorithm Dijkstra’s Algorithm A V Ali :
Dijkstra animation. Dijksta’s Algorithm (Shortest Path Between 2 Nodes) 2 Phases:initialization;iteration Initialization: 1. Included:(Boolean) 2. Distance:(Weight)
CSE 373: Data Structures and Algorithms Lecture 21: Graphs V 1.
Shortest Paths.
Graphs Chapter 15 introduces graphs which are probably the most general and commonly-used data structure. This lecture introduces heaps, which are used.
By Laksman Veeravagu and Luis Barrera
Shortest Paths and Minimum Spanning Trees
Chapter 7: Greedy Algorithms
Single-Source Shortest Paths
Routing: Distance Vector Algorithm
Shortest Path Problems
Routing in Packet Networks Shortest Path Routing
Comp 245 Data Structures Graphs.
Discussion section #2 HW1 questions?
Graphs & Graph Algorithms 2
Graphs Chapter 13.
Shortest Paths.
Shortest Path.
Shortest Path.
A* Path Finding Ref: A-star tutorial.
CSE 373: Data Structures and Algorithms
Outline This topic covers Prim’s algorithm:
Graph Operations And Representation
Shortest Path Problems
Shortest Path Algorithms
Shortest Paths and Minimum Spanning Trees
Chapter 15 Graphs © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Slide Courtesy: Uwash, UT
Advanced Computer Networks
CSE 373: Data Structures and Algorithms
Shortest Path.
CSE 373 Data Structures and Algorithms
Slide Courtesy: Uwash, UT
Shortest Paths.
Graphs.
Graphs.
Graphs.
Graphs.
Graphs: Shortest path and mst
Chapter 9 Graph algorithms
Presentation transcript:

Graphs – Part III CS 367 – Introduction to Data Structures

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?

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

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

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

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(); } }

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; }

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

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

Dijkstra’s Algorithm Iteration B2---- C33--- D∞55-- E∞∞∞∞12 F∞10 7- A B D C E F Distance Table for A Distance First Hop BCBBB B C D E F

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

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 }

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

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

Bellman-Ford Algorithm Iteration 123 B222 C333 D∞55 E∞∞12 F∞77 A B D C E F Distance Table for A Distance First Hop BCBBB B C D E F

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) }

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