Fundamental Structures of Computer Science II

Slides:



Advertisements
Similar presentations
Chapter 9: Graphs Shortest Paths
Advertisements

CS 206 Introduction to Computer Science II 04 / 01 / 2009 Instructor: Michael Eckmann.
§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.
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
Graph Algorithms. Jaruloj Chongstitvatana Chapter 3: Greedy Algorithms 2 Outline Graph Representation Shortest path Minimum spanning trees.
§2 Topological Sort 〖 Example 〗 Courses needed for a computer science degree at a hypothetical university How shall we convert this list into a graph?
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.
Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 CHAPTER 4 - PART 2 GRAPHS 1.
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,
1 Graph Theory © Dave Bockus. 2 Adjacency Matrix
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.
Shortest Paths and Dijkstra's Algorithm CS 110: Data Structures and Algorithms First Semester,
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.
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.
CS2420: Lecture 37 Vladimir Kulyukin Computer Science Department Utah State University.
CS 206 Introduction to Computer Science II 11 / 10 / 2008 Instructor: Michael Eckmann.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
Graphs & Graph Algorithms 2 Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Shortest Path Algorithm By Weston Vu CS 146. What is Shortest Paths? Shortest Paths is a part of the graph algorithm. It is used to calculate the shortest.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
CSE 326: Data Structures Graphs Ben Lerner Summer 2007.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
CSC 2300 Data Structures & Algorithms April 3, 2007 Chapter 9. Graph Algorithms.
1 Shortest Path Calculations in Graphs Prof. S. M. Lee Department of Computer Science.
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
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.
Computer Science 112 Fundamentals of Programming II Graph Algorithms.
1 WEEK 9-10 Graphs II Unweighted Shortest Paths Dijkstra’s Algorithm Graphs with negative costs Acyclic Graphs Izmir University of Economics.
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.
Lecture 12-2: Introduction to Computer Algorithms beyond Search & Sort.
Graph Algorithms Terminology Topological sort Shortest-path algorithms
ALG0183 Algorithms & Data Structures Lecture 21 d Dijkstra´s algorithm 8/25/20091 ALG0183 Algorithms & Data Structures by Dr Andy Brooks Chapter 14 Weiss.
Shortest Paths and Dijkstra’s Algorithm CS 105. SSSP Slide 2 Single-source shortest paths Given a weighted graph G and a source vertex v in G, determine.
CS 61B Data Structures and Programming Methodology Aug 5, 2008 David Sun.
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.
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.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Graph Algorithms CS 202 – Fundamental Structures of Computer Science.
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)
CSC317 Shortest path algorithms
C.Eng 213 Data Structures Graphs Fall Section 3.
Unweighted Shortest Path Neil Tang 3/11/2010
Graphs.
Shortest Path Graph represents highway system Edges have weights
CSC 172 DATA STRUCTURES.
Graphs & Graph Algorithms 2
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.
CSE 373: Data Structures and Algorithms
Shortest Path Algorithms
CMSC 341 Lecture 20.
Shortest Path Algorithms
Topological Sort Algorithm
Fundamental Structures of Computer Science II
CE 221 Data Structures and Algorithms
Graphs G = (V, E) V are the vertices; E are the edges.
CSE 417: Algorithms and Computational Complexity
CMSC 341 Graphs 2.
CE 221 Data Structures and Algorithms
Lecture 12 Shortest Path.
Richard Anderson Spring 2016
Graphs: Shortest path and mst
Chapter 9: Graphs Shortest Paths
GRAPH – Definitions A graph G = (V, E) consists of
Directed Graphs (Part II)
Presentation transcript:

Fundamental Structures of Computer Science II Graph Algorithms - 2 CS 202 – Fundamental Structures of Computer Science II Bilkent University Computer Engineering Department CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

Shortest Path Algorithms Input is weighted graph: Associated with edge (vi, vj), there is a cost ci,j to traverse the edge. The cost of a path v1v2…vN is: Unweighted path length is simply the number of edges on the path, namely N-1. Weighted Path Length CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

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. 2 v1 v2 10 4 1 3 2 v3 v4 2 v5 8 4 6 5 1 v6 v7 Shortest (Weighted) Path from V1 to V6 is: v1, v4, v7, v6. Cost=6 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

Single-Source Shortest Path Problem We will examine different algorithms 1) Unweighted shortest path algorithm 2) Weighted shortest path algorithm Assuming no negative edges. CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

Unweighted Shortest Paths v1 v2 v3 v4 v5 v6 v7 Assume all edges are unweighted. We are interested in all shortest paths to all nodes from a given node, s. Example: assume v3 is the node from where we will find all the shortest paths. CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

Unweighted Shortest Paths v1 v2 v3 v4 v5 v6 v7 Assume all edges are unweighted. We are interested in all shortest path to all nodes from a given node, s. Example: assume v3 is the node from where we will find all the shortest paths. CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

Fundamental Structures of Computer Science II Algorithm Sketch 1. Start with initial node s. Mark the distance of s to s as 0. 2. Find all nodes adjacent to s. For all these nodes: Mark their distances to s as distances of previous nodes + 1. 3. Repeat step 2 for all adjacent nodes. Stop when you exhausted all the nodes (vertices). CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

Example We want to find out all the shortest paths to all nodes from node v3 v1 v2 S v3 v4 v5 v6 v7 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

Fundamental Structures of Computer Science II Vertex Adjacent Vertices v3 v1, v6 1 v1 v2 S v3 v3 v4 v5 v6 v7 1 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

Fundamental Structures of Computer Science II Vertex Adjacent Vertices v1 v2, v4 2 v1 v1 v2 1 S v3 v3 v4 v5 2 1 v6 v7 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

Fundamental Structures of Computer Science II Vertex Adjacent Vertices v2 v4, v5 2 v1 v1 v2 v2 1 S v3 v3 v4 v5 3 2 1 v6 v7 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

Fundamental Structures of Computer Science II Vertex Adjacent Vertices v4 v6, v7 2 v1 v1 v2 v2 1 S v3 v3 v4 v4 v5 3 2 1 v6 v7 3 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

Fundamental Structures of Computer Science II Vertex Adjacent Vertices v5 v4, v7 2 v1 v1 v2 v2 1 S v3 v3 v4 v4 v5 v5 3 2 1 v6 v7 3 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

Fundamental Structures of Computer Science II Vertex Adjacent Vertices v6 none v7 2 v1 v1 v2 v2 1 S v3 v3 v4 v4 v5 v5 3 2 1 v6 v6 v7 v7 3 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

Algorithm – Initial Configuration vertex known Distance to S Previous node v1 F ∞ v2 v3 v4 V5 V6 V7 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

Fundamental Structures of Computer Science II void Graph::unweighted_shortest_paths(vertex s) { Queue q(NUM_VERTICES); Vertex v,w; q.enqueue(s); s.dist = 0; while (!q.isEmpty()) v= q.dequeue(); v.known = true; // not needed anymore for each w adjascent to v if (w.dist == INFINITY) w.dist = v.dist + 1; w.path = v; q.enqueue(w); } CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

Fundamental Structures of Computer Science II Final Configuration vertex known Distance to S Previous node v1 T 1 v3 v2 2 v4 V5 3 V6 V7 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

Weighted Shortest Paths Dijkstra’s Algorithm Example of a greedy algorithm Do the best thing in each step. At each state, the algorithm chooses a vertex v, which has the smallest distance to s (dv) among all the unknown vertices. Then the adjacent nodes of v (which are denoted as w) are updated with new distance information. CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

Example: Weighted Directed Graph G is shown! 2 v1 v2 10 4 1 3 2 v3 v4 2 v5 8 4 6 5 1 v6 v7 We are interested in all shortest paths to all nodes from node v1 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

Initial Configuration vertex known Distance to S Previous node v1 F v2 ∞ v3 v4 V5 V6 V7 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

Fundamental Structures of Computer Science II ∞ 2 v1 v2 10 4 1 3 ∞ 2 2 v3 v4 v5 ∞ ∞ 8 4 6 5 1 v6 v7 ∞ ∞ CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

Fundamental Structures of Computer Science II 2 2 v1 v1 v2 10 4 1 3 ∞ 2 2 v3 v4 v5 ∞ 1 8 4 6 5 1 v6 v7 ∞ ∞ CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

Fundamental Structures of Computer Science II 2 2 v1 v1 v2 10 4 1 3 3 2 2 v3 v4 v4 v5 3 1 8 4 6 5 1 v6 v7 9 5 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

Fundamental Structures of Computer Science II 2 2 v1 v1 v2 v2 10 4 1 3 3 2 2 v3 v4 v4 v5 3 1 8 4 6 5 1 v6 v7 9 5 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

Fundamental Structures of Computer Science II 2 2 v1 v1 v2 v2 10 4 1 3 3 2 2 v3 v4 v4 v5 v5 3 1 8 4 6 5 1 v6 v7 9 5 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

Fundamental Structures of Computer Science II 2 2 v1 v1 v2 v2 10 4 1 3 3 2 2 v3 v3 v4 v4 v5 v5 3 1 8 4 6 5 1 v6 v7 8 9 5 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

Fundamental Structures of Computer Science II 2 2 v1 v1 v2 v2 10 4 1 3 3 2 2 v3 v3 v4 v4 v5 v5 3 1 8 4 6 5 1 v6 v7 v7 8 6 5 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

Fundamental Structures of Computer Science II 2 2 v1 v1 v2 v2 10 4 1 3 3 2 2 v3 v3 v4 v4 v5 v5 3 1 8 4 6 5 1 v6 v6 v7 v7 6 5 Finished! CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

Fundamental Structures of Computer Science II Final Configuration vertex known Distance to S Previous node v1 T v2 2 v3 3 v4 1 V5 V6 6 v7 V7 5 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

Implementation of Algorithm struct Vertex { List adj; // adjacency list boolean known; // if we are finished processing the node int dist; // distance to source. Vertex path; // the previous node towards the source } void Graph::createTable( vector<Vertex> &t) { readGraph(t); for (int i=0; i<t.size(); i++) t[i].known = FALSE; t[i].dist = INFINITY; t[i].path = NOT_A_VERTEX; //NULL } NUM_VERTICES = t.size(); CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University

Fundamental Structures of Computer Science II void Graph::dijkstra( vector<Vertex> &s) { Vertex v, w; s.dist = 0; for ( ; ; ) v = an unknown vertex whose distance to s is minimum; if (v == NOT_A_VERTEX) break; // we are finished v.known = TRUE; for each w adjacent to v if (w.known == FALSE) if (v.dist + cost_v_w < w.dist) // update w w.dist = v.dist + cost_v_w; w.path = v; } CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University