CMSC 341 Graphs 2. 2 Weighted Shortest Path Problem Single-source shortest-path problem: Given as input a weighted graph, G = (V,E), and a distinguished.

Slides:



Advertisements
Similar presentations
The Greedy Approach Chapter 8. The Greedy Approach It’s a design technique for solving optimization problems Based on finding optimal local solutions.
Advertisements

Graph Algorithms. Jaruloj Chongstitvatana Chapter 3: Greedy Algorithms 2 Outline Graph Representation Shortest path Minimum spanning trees.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graph Traversals Visit vertices of a graph G to determine some property: Is G connected? Is there a path from vertex a to vertex b? Does G have a cycle?
Chapter 8, Part I Graph Algorithms.
1 Graphs: Traversal Searching/Traversing a graph = visiting the vertices of a graph by following the edges in a systematic way Example: Given a highway.
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
1 9.3 Shortest Path Algorithms Improvement –Use a queue to keep all vertices whose shortest distance to s is known. –Initialize d v to  for all vertices,
The Shortest Path Problem. Shortest-Path Algorithms Find the “shortest” path from point A to point B “Shortest” in time, distance, cost, … Numerous.
1 Shortest Path Algorithms Given a graph G = (V, E) and a distinguished vertex s, find the shortest weighted path from s to every other vertex in G. weighted.
1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320.
Chapter 23 Minimum Spanning Trees
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.
CSE 780 Algorithms Advanced Algorithms Graph Algorithms Representations BFS.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Dijkstra’s Algorithm Slide Courtesy: Uwash, UT 1.
CSC 2300 Data Structures & Algorithms April 3, 2007 Chapter 9. Graph Algorithms.
TECH Computer Science Graph Optimization Problems and Greedy Algorithms Greedy Algorithms  // Make the best choice now! Optimization Problems  Minimizing.
Dijkstra's algorithm.
CS 146: Data Structures and Algorithms July 21 Class Meeting
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.
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 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,
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.
ALG0183 Algorithms & Data Structures Lecture 21 d Dijkstra´s algorithm 8/25/20091 ALG0183 Algorithms & Data Structures by Dr Andy Brooks Chapter 14 Weiss.
COSC 2007 Data Structures II Chapter 14 Graphs III.
Graphs. What is a graph? A data structure that consists of a set of nodes (vertices) and a set of edges that relate the nodes to each other The set of.
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.
CMSC 380 Graph Traversals and Search. 2 Graph Traversals Graphs can be traversed breadth-first, depth- first, or by path length We need to specifically.
CMSC 341 Graphs. 8/3/2007 UMBC CMSC 341 Graphs 2 Basic Graph Definitions A graph G = (V,E) consists of a finite set of vertices, V, and a finite set of.
Graph and Digraph Sung Yong Shin TC Lab. CS Dept., KAIST.
 Hamilton paths.  Definition 20: A Hamilton paths is a path that contains each vertex exactly once. A Hamilton circuit is a circuit that contains.
Graphs + Shortest Paths David Kauchak cs302 Spring 2013.
Graph Theory Def: A graph is a set of vertices and edges G={V,E} Ex. V = {a,b,c,d,e} E = {ab,bd,ad,ed,ce,cd} Note: above is a purely mathematical definition.
Spanning Trees Dijkstra (Unit 10) SOL: DM.2 Classwork worksheet Homework (day 70) Worksheet Quiz next block.
CMSC 341 Graphs. 5/5/20062 Basic Graph Definitions A graph G = (V,E) consists of a finite set of vertices, V, and a finite set of edges, E. Each edge.
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.
CMSC 341 Graphs. 2 Basic Graph Definitions A graph G = (V,E) consists of a finite set of vertices, V, and a set of edges, E. Each edge is a pair (v,w)
Chapter 22 Elementary Graph Algorithms
CSE 373 Topological Sort Graph Traversals
CMSC 341 Graphs.
Introduction to Graphs
Shortest Path Problems
Graphs.
CMSC 341 Lecture 21 Graphs (Introduction)
CSC 172 DATA STRUCTURES.
Graph Algorithm.
CSE 421: Introduction to Algorithms
CMSC 341 Graphs.
MST - Prim’s Algorithm Greedy algorithm that “grows” an MST by repeatedly finding the lowest cost edge that will connect a new vertex to MST For every.
Graphs.
Shortest Path Algorithms
Shortest Path Problems
CMSC 341 Lecture 20.
Shortest Path Algorithms
Topological Sort Algorithm
Fundamental Data Structures and Algorithms
Fundamental Structures of Computer Science II
GRAPHS G=<V,E> Adjacent vertices Undirected graph
Fundamental Structures of Computer Science II
CSE 417: Algorithms and Computational Complexity
CMSC 341 Graphs 2.
Data structure for graph algorithms: Adjacent list, Adjacent matrix
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:

CMSC 341 Graphs 2

2 Weighted Shortest Path Problem 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. Use Dijkstra’s algorithm –keep tentative distance for each vertex giving shortest path length using vertices visited so far –keep vertex before this vertex (to allow printing of path) –at each step choose the vertex with smallest distance among the unvisited vertices (greedy algorithm)

3 Dijkstra’s Algorithm Vertex v, w; Initialize all vertices as unknown with distance = INFINITY start.dist = 0; while there are unknown vertices v = smallest unknown distance vertex; v.known = TRUE; for each w adjacent to v if (!w.known) if (v.dist + cvw < w.dist) { decrease (w.dist to v.dist + cvw); w.path = v; }

4 Dijkstra Example a b c d g e ji fh

5 Traversal Performance What is the performance of DF and BF traversal? Each vertex appears in the stack or queue exactly once. Therefore, the traversals are at least O(|V|). However, at each vertex, we must find the adjacent vertices. Therefore, df- and bf-traversal performance depends on the performance of the getAdjacent operation.

6 getAdjacent Method 1: Look at every vertex (except u), asking “are you adjacent to u?” List L = new List( ); for (each vertex v, except u) if (isAdjacentTo(u,v)) L.doInsert(v); Assuming O(1) performance on isAdjacentTo, getAdjacent has O(|V|) performance and traversal performance is O(|V 2 |);

7 getAdjacent (cont) Method 2: Look only at the edges which impinge on u. Therefore, at each vertex, the number of vertices to be looked at is D(u), the degree of the vertex (use outdegree for directed graph). This approach is O(D(u)). The traversal performance is since getAdjacent is done O(|V|) times. However, in a disconnected graph, we must still look at every vertex, so the performance is O(max(|V|, |E|)) = O(|V| + |E|). But, what is O(|E|) ?? ))(( 1 vDO V i i    O ( |E| )

8 Number of Edges Theorem: The number of edges in an undirected graph G=(V,E) is O(|V| 2 ) Proof: Suppose G is fully connected. Let p =|V|. We have the following situation: vertexconnected to 12,3,4,5,…, p 21,3,4,5,…, p … p1,2,3,4,…,p-1 –There are p(p-1)/2 = O(|V| 2 ) edges. So O(|E|) = O(|V| 2 ).