CE 221 Data Structures and Algorithms

Slides:



Advertisements
Similar presentations
§3 Shortest Path Algorithms Given a digraph G = ( V, E ), and a cost function c( e ) for e  E( G ). The length of a path P from source to destination.
Advertisements

CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
CSC 2300 Data Structures & Algorithms April 13, 2007 Chapter 9. Graph Algorithms.
§2 Topological Sort 〖 Example 〗 Courses needed for a computer science degree at a hypothetical university How shall we convert this list into a graph?
Graph II MST, Shortest Path. Graph Terminology Node (vertex) Edge (arc) Directed graph, undirected graph Degree, in-degree, out-degree Subgraph Simple.
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
Directed Graph Algorithms CSE 373 Data Structures Lecture 14.
Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Dijkstra’s Algorithm Slide Courtesy: Uwash, UT 1.
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
Graphs CS 400/600 – Data Structures. Graphs2 Graphs  Used to represent all kinds of problems Networks and routing State diagrams Flow and capacity.
CS 146: Data Structures and Algorithms July 21 Class Meeting
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms Shortest-Path.
1 Graphs Algorithms Sections 9.1, 9.2, and Graphs v1v1 v2v2 v5v5 v7v7 v8v8 v3v3 v6v6 v4v4 A graph G = (V, E) –V: set of vertices (nodes) –E: set.
1 WEEK 9-10 Graphs II Unweighted Shortest Paths Dijkstra’s Algorithm Graphs with negative costs Acyclic Graphs Izmir University of Economics.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
1 Chapter 9 Graph Algorithms Real-life graph problems Algorithms for some graph problems Choice of data structures for graph problems.
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 10 Prepared by İnanç TAHRALI.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
CSE 373: Data Structures and Algorithms Lecture 21: Graphs V 1.
Chapter 22 Elementary Graph Algorithms
CSE 373 Topological Sort Graph Traversals
Chapter 9 : Graphs Part II (Minimum Spanning Trees)
CSE 2331/5331 Topic 9: Basic Graph Alg.
C.Eng 213 Data Structures Graphs Fall Section 3.
Introduction to Graphs
Depth-First Search.
Common final examinations
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
Graphs.
CS202 - Fundamental Structures of Computer Science II
Graphs Graph transversals.
Graph & BFS.
Graphs Chapter 11 Objectives Upon completion you will be able to:
CSE373: Data Structures & Algorithms Lecture 18: Dijkstra’s Algorithm
Basic Graph Algorithms
Shortest Path Algorithms
Chapter 11 Graphs.
CMSC 341 Lecture 20.
Shortest Path Algorithms
Directed Graph Algorithms
Directed Graph Algorithms
ITEC 2620M Introduction to Data Structures
Chapter 15 Graphs © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Slide Courtesy: Uwash, UT
CSE 373: Data Structures and Algorithms
Shortest Path Algorithm for Weighted Non-negative Undirected Graphs
CSE332: Data Abstractions Lecture 17: Shortest Paths
Graph Implementation.
CE 221 Data Structures and Algorithms
CSE 373 Data Structures and Algorithms
Slide Courtesy: Uwash, UT
Data Structures and Algorithm Analysis Graph Algorithms
CSE 417: Algorithms and Computational Complexity
Dijkstra's Shortest Path Algorithm
Graphs.
Graphs.
CSE 373: Data Structures and Algorithms
Graphs.
Elementary Graph Algorithms
Graphs.
Graphs: Shortest path and mst
GRAPH – Definitions A graph G = (V, E) consists of
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
More Graphs Lecture 19 CS2110 – Fall 2009.
Presentation transcript:

CE 221 Data Structures and Algorithms Chapter 9 : Graphs Part I (Topological Sort & Shortest Path Algorithms) Text: Read Weiss, §9.1 – 9.3 Izmir University of Economics

Izmir University of Economics Definitions - I A graph G=(V, E) consists of a set of vertices, V, and a set of edges, E. Each edge is a pair (v, w), where v, w є V. If the pair is ordered then G is directed (digraph). Vertex w is adjacent to v iff (v, w) є E. In an undirected graph with edge (v, w), w is adjacent to v and v is adjacent to w. Sometimes and edge has a third component, weight or cost. Izmir University of Economics

Izmir University of Economics Definitions - II A path in a graph is w1, w2,...,wN such that (wi, wi+1) є E for 1≤i<N. The length of such a path is the number of edges on the path. If a path from a vertex to itself contains no edges, then the path length is zero. If G contains an edge (v, v), then the path v, v is called a loop. A simple path is a path such that all vertices are distinct, except that the first and the last could be the same. Izmir University of Economics

Izmir University of Economics Definitions - III A cycle in a directed graph is a path of length at least 1 such that w1=wN. This cycle is simple if the path is simple. For undirected graphs, the edges are required to be distinct (Why?). A directed graph is acyclic if it has no cycles (DAG). An undirected graph is connected if there is a path from every vertex to every other vertex. A directed graph with this property is called strongly connected. If directed graph is not, but underlying undirected graph is, it is weakly connected. A complete graph is a graph in which there is an edge between every pair of vertices. Izmir University of Economics

Representation of Graphs - I One simple way is to use a two-dimensional array (adjacency matrix representation). If vertices are numbered starting at 1, A[u][v]=true if (u, v) є E. Space requirement is Θ(|V|2). If the graph is not dense (sparse), adjacency lists may be used. The space requirement is O(|E|+|V|). Izmir University of Economics

Graph Terminology (continued)

Graph Terminology (continued)

Graph Terminology (continued)

Graph Terminology (continued) a vertex v is called the out-degree of the vertex. The number of the edges that terminate in vertex v is the in-degree of the vertex.

Graph Traversal Algorithms In general, graphs do not have a vertex, like a root, that initiates unique paths to each of the vertices. From any starting vertex in a graph, it might not be possible to search all of the vertices. A graph could have a cycle that results in multiple visits to a vertex.

2 Major Traversal Algorithms Breadth-first search visits vertices in the order of their path length from a starting vertex. It may not visit every vertex of the graph Depth-first search traverses all the vertices of a graph by making a series of recursive calls that follow paths through the graph.

Izmir University of Economics Topological Sort - I A topological sort is an ordering of vertices in a DAG, such that if there is path from vi to vj, then vj appears after vi in the ordering. A simple algorithm to find a topological ordering is first to find any vertex with no incoming edges. We can then print this vertex, and remove it, along with its edges. Then apply the same strategy to the rest of the graph. To formalize this, define the indegree of a vertex v as the number of edges (u, v). Izmir University of Economics

Topological Sort – Initial Attempt running time of the algorithm is O(|V|2). Izmir University of Economics

Topological Sort – A Better Algorithm We can remove the inefficiency by keeping all the unassigned vertices of indegree 0 in a special data structure (queue or stack). When a new vertex with degree zero is needed, it is returned by removing one from the queue, and when the indegrees of adjacent vertices are decremented, they are inserted into the queue if the indegree falls to zero. The running time is O(|E|+|V|) Izmir University of Economics

Izmir University of Economics Homework Assignments 9.1, 9.2, 9.3, 9.4, 9.38 You are requested to study and solve the exercises. Note that these are for you to practice only. You are not to deliver the results to me. Izmir University of Economics

Shortest-Path Algorithms The input is a weighted graph: associated with each edge (vi, vj) is a cost ci,j. The cost of a path v1v2...vN is ∑ci,i+1 for i in [1..N-1]. This is weighted path length. Unweighted path length is merely the number of edges on the path, namely, N-1. Single-source Shortest-Path Problem: Given as input a weighted graph G=(V, E), and a distinguished vertex, s, find the shortest weighted path from s to every other vertex in G. Izmir University of Economics

Izmir University of Economics Negative Cost Cycles In the graph to the left, the shortest path from v1 to v6 has a cost of 6 and the path itself is v1v4v7v6. The shortest unweighted path has 2 edges (v1 - v4) and (v4 - v6). In the graph to the right, we have a negative cost. The path from v5 to v4 has cost 1, but a shorter path exists by following the loop v5v4v2v5v4 which has cost -5. This path is still not the shortest, because we could stay in the loop arbitrarily long. Izmir University of Economics

Shortest Path Length: Problems We will examine 4 algorithms to solve four versions of the problem Unweighted shortest path  O(|E|+|V|) Weighted shortest path without negative edges  O(|E|log|V|) using queues Weighted shortest path with negative edges  O(|E| . |V|) Weighted shortest path of acyclic graphs  linear time Izmir University of Economics

Unweighted Shortest Paths Using some vertex, s, which is an input parameter, find the shortest path from s to all other vertices in an unweighted graph. Assume s=v3. Izmir University of Economics

Unweighted Shortest Paths Algorithm: find vertices that are at distance 1, 2, ... N-1 by processing vertices in layers (breadth-first search) Izmir University of Economics

Unweighted Shortest Paths Izmir University of Economics

Unweighted Shortest Paths Complexity O(|V|2) Izmir University of Economics

Unweighted Shortest Paths - Improvement At any point in time there are only two types of unknown vertices that have dv≠∞. Some have dv = currDist and the rest have dv = currDist +1. We can make use of a queue data structure. O(|E|+|V|) Izmir University of Economics

Weighted Shortest Path Dijkstra’s Algorithm With weighted shortest path,distance dv is tentative. It turns out to be the shortest path length from s to v using only known vertices as intermediates. Greedy algorithm: proceeds in stages doing the best at each stage. Dijkstra’s algorithm selects a vertex v with smallest dv among all unknown vertices and declares it known. Remainder of the stage consists of updating the values dw for all edges (v, w). Izmir University of Economics

Dijkstra’s Algorithm - Example ► ► ► Izmir University of Economics

Dijkstra’s Algorithm - Example ► ► ► ► A proof by contradiction will show that this algorithm always works as long as no edge has a negative cost. Izmir University of Economics

Dijkstra’s Algorithm - Pseudocode If the vertices are sequentially scanned to find minimum dv, each phase will take O(|V|) to find the minimum, thus O(|V|2) over the course of the algorithm. The time for updates is constant and at most one update per edge for a total of O(|E|). Therefore the total time spent is O(|V|2+|E|). If the graph is dense, OPTIMAL. Izmir University of Economics

Dijkstra’s Algorithm-What if the graph is sparse? If the graph is sparse |E|=θ(|V|), algorithm is too slow. The distances of vertices need to be kept in a priority queue. Selection of vertex with minimum distance via deleteMin, and updates via decreaseKey operation. Hence; O(|E|log|V|+|V|log|V|) find operations are not supported, so you need to be able to maintain locations of di in the heap and update them as they change. Alternative: insert w and dw with every update. Izmir University of Economics

Graphs with negative edge costs Dijkstra’s algorithm does not work with negative edge costs. Once a vertex u is known, it is possible that from some other unknown vertex v, there is a path back to u that is very negative. Algorithm: A combination of weighted and unweighted algorithms. Forget about the concept of known vertices. Izmir University of Economics

Graphs with negative edge costs - I O(|E|*|V|). Each vertex can dequeue at most O(|V|) times. (Why? Algorithm computes shortest paths with at most 0, 1, ..., |V|-1 edges in this order). Hence, the result! If negative cost cycles, then each vertex should be checked to have been dequeued at most |V| times. Izmir University of Economics

Izmir University of Economics Acyclic Graphs If the graph is known to be acyclic, the order in which vertices are declared known, can be set to be the topological order. Running time = O(|V|+|E|) This selection rule works because when a vertex is selected, its distance can no longer be lowered, since by topological ordering rule it has no incoming edges emanating from unknown nodes. Izmir University of Economics

Dijkstra's Shortest Path Algorithm Find shortest path from s to t. 2 24 3 9 s 18 14 2 6 6 4 30 19 11 15 5 5 6 20 16 t 7 44

Dijkstra's Shortest Path Algorithm PQ = { s, 2, 3, 4, 5, 6, 7, t }   2 24 3 9 s 18 14  2 6 6  4 30  19 11 15 5 5 6 20 16 t 7 44  distance label 

Dijkstra's Shortest Path Algorithm PQ = { s, 2, 3, 4, 5, 6, 7, t } Here   2 24 3 9 s 18 14  2 6 6  30  4 19 11 15 5 5 6 20 16 t 7 44  distance label 

Dijkstra's Shortest Path Algorithm PQ = { 2, 3, 4, 5, 6, 7, t } Node discovered   X 9 2 24 3 9 s 18 14  X 14 2 6 6  4 30  19 11 15 5 5 6 20 16 t 7 44  distance label  15 X

Dijkstra's Shortest Path Algorithm PQ = { 2, 3, 4, 5, 6, 7, t } Here   X 9 2 24 3 9 s 18 14  X 14 2 6 6  4 30  19 11 15 5 5 6 20 16 t 7 44  distance label  15 X

Dijkstra's Shortest Path Algorithm PQ = { 3, 4, 5, 6, 7, t }   X 9 2 24 3 9 s 18 14  X 14 2 6 6  30  4 19 11 15 5 5 6 20 16 t 7 44   15 X

Dijkstra's Shortest Path Algorithm PQ = { 3, 4, 5, 6, 7, t } Node discovered  X 33  X 9 2 24 3 9 s 18 14  X 14 2 6 6   4 30 19 11 15 5 5 6 20 16 t 7 44   15 X

Dijkstra's Shortest Path Algorithm PQ = { 3, 4, 5, 6, 7, t }  X 33  X 9 2 24 3 9 Here s 18 14  X 14 2 6 6   4 30 19 11 15 5 5 6 20 16 t 7 44   15 X

Dijkstra's Shortest Path Algorithm PQ = { 3, 4, 5, 7, t } 32  X 33 X  X 9 2 24 3 9 s 18 14  X 14 2 6 6  44 30  X 4 19 11 15 5 5 6 20 16 t 7 44   15 X

Dijkstra's Shortest Path Algorithm PQ = { 3, 4, 5, 7, t } 32  X 33 X  X 9 2 24 3 9 s 18 14  X 14 2 6 6  44 30  X 4 19 11 15 5 5 6 20 16 t 7 44   15 Here X

Dijkstra's Shortest Path Algorithm PQ = { 3, 4, 5, t } 32  X 33 X  X 9 2 24 3 9 s 18 14  X 14 2 6 6  44 X 35  X 4 30 19 11 15 5 5 6 20 16 t 7 44 59   15 X X

Dijkstra's Shortest Path Algorithm PQ = { 3, 4, 5, t } Here 32  X 33 X  X 9 2 24 3 9 s 18 14  X 14 2 6 6  44 X 35 4 30  X 19 11 15 5 5 6 20 16 t 7 44 59   15 X X

Dijkstra's Shortest Path Algorithm PQ = { 4, 5, t } 32  X 33 X  X 9 2 24 3 9 s 18 14  X 14 2 6 6  44 X 35 X 34 4 30  X 19 11 15 5 5 6 20 16 t 7 44 51 59   15 X X X

Dijkstra's Shortest Path Algorithm PQ = { 4, 5, t } 32  X 33 X  X 9 2 24 3 9 s 18 14  X 14 2 6 6  44 X 35 X 34 30  X 4 19 11 15 5 5 6 20 Here 16 t 7 44 51 59   15 X X X

Dijkstra's Shortest Path Algorithm PQ = { 4, t } 32  X 33 X  X 9 2 24 3 9 s 18 14  X 14 2 6 6 45  44 X 35 X 34 X 30  X 4 19 11 15 5 5 6 20 16 t 7 44 50 51 X 59   15 X X X

Dijkstra's Shortest Path Algorithm PQ = { 4, t } 32  X 33 X  X 9 2 24 3 9 s 18 14  X 14 2 6 6 45  44 X 35 X 34 X 30  X 4 19 11 15 5 Here 5 6 20 16 t 7 44 50 51 X 59   15 X X X

Dijkstra's Shortest Path Algorithm PQ = { t } 32  X 33 X  X 9 2 24 3 9 s 18 14  X 14 2 6 6 45  44 X 35 X 34 X 30  X 4 19 11 15 5 5 6 20 16 t 7 44 50 51 X 59   15 X X X

Dijkstra's Shortest Path Algorithm PQ = { t } 32  X 33 X  X 9 2 24 3 9 s 18 14  X 14 2 6 6 45  44 X 35 X 34 X 30  X 4 19 11 15 5 5 6 20 16 t 7 44 Here 50 51 X 59   15 X X X

Dijkstra's Shortest Path Algorithm S = { s, 2, 3, 4, 5, 6, 7, t } PQ = { } 32  X 33 X  X 9 2 24 3 9 s 18 14  X 14 2 6 6 45  44 X 35 X 34 X  X 4 30 19 11 15 5 5 6 20 16 t 7 44 50 51 X 59   15 X X X

Dijkstra's Shortest Path Algorithm S = { s, 2, 3, 4, 5, 6, 7, t } PQ = { } 32  X 33 X  X 9 2 24 3 9 s 18 14  X 14 2 6 6 45  44 X 35 X 34 X  X 4 30 19 11 15 5 5 6 20 16 t 7 44 50 51 X 59   15 X X X

The Algoritm 1. Assign to every node a distance value. Set it to zero for our initial node and to infinity (-9999) for all other nodes. 2. Mark all nodes as unvisited. Set initial node as current. 3. For current node, consider all its unvisited neighbors and calculate their distance (from the initial node). For example, if current node (A) has distance of 6, and an edge connecting it with another node (B) is 2, the distance to B through A will be 6+2=8. If this distance is less than the previously recorded distance (infinity in the beginning, zero for the initial node), overwrite the distance. 4. When we are done considering all neighbors of the current node, mark it as visited. A visited node will not be checked ever again; its distance recorded now is final and minimal. 5. If all nodes have been visited, finish. Otherwise, set the unvisited node with the smallest distance (from the initial node) as the next "current node" and continue from step 3.

Pseudo Code of the Dijkstra’s Alg Algorithm Dijkstra(Graph, source): for each vertex v in Graph: // Initializations dist[v] := infinity // Unknown distance function from source to v previous[v] := undefined // Previous node in optimal path from source dist[source] := 0 // Distance from source to source Q := the set of all nodes in Graph // All nodes in the graph are unoptimized - thus are in Q while Q is not empty: // The main loop u:= dequeue (Q) // u := vertex in Q with smallest dist[] if dist[u] = infinity: break // all remaining vertices are inaccessible from source dequeue(u,Q) // remove u from Q for each neighbor v of u: // where v has not yet been removed from Q. alt := dist[u] + dist_between(u, v) // Dist. from start point to neighbor if alt < dist[v]: // Relax (u,v) dist[v] := alt previous[v] := u return dist

Izmir University of Economics Homework Assignments 9.5, 9.7, 9.10, 9.40, 9.42, 9.44, 9.46, 9.47, 9.52 You are requested to study and solve the exercises. Note that these are for you to practice only. You are not to deliver the results to me. Izmir University of Economics