CSC 331: Algorithm Analysis Paths in Graphs. The DFS algorithm we gave is recursive. DFS generates a search tree showing paths from one vertex to all.

Slides:



Advertisements
Similar presentations
Single Source Shortest Paths
Advertisements

CS 206 Introduction to Computer Science II 04 / 01 / 2009 Instructor: Michael Eckmann.
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
Nattee Niparnan. Distance of nodes  The distance between two nodes is the length of the shortest path between them S  A 1 S  C 1 S  B 2.
The Greedy Approach Chapter 8. The Greedy Approach It’s a design technique for solving optimization problems Based on finding optimal local solutions.
Topological Sort Topological sort is the list of vertices in the reverse order of their finishing times (post-order) of the depth-first search. Topological.
Graph Theory, DFS & BFS Kelly Choi What is a graph? A set of vertices and edges –Directed/Undirected –Weighted/Unweighted –Cyclic/Acyclic.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graph A graph, G = (V, E), is a data structure where: V is a set of vertices (aka nodes) E is a set of edges We use graphs to represent relationships among.
1 Dijkstra’s Minimum-Path Algorithm Minimum Spanning Tree CSE Lectures 20 – Intro to Graphs.
CSE 101- Winter ‘15 Discussion Section January 26th 2015.
Breadth-First and Depth-First Search
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© K.Goczyła GRAPHS Definitions and data structuresDefinitions and data structures Traversing graphsTraversing graphs Searching for paths in graphsSearching.
CSCI 3160 Design and Analysis of Algorithms Tutorial 2 Chengyu Lin.
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.
CSE332: Data Abstractions Lecture 16: Topological Sort / Graph Traversals Tyler Robison Summer
Lecture 19: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance
CS2420: Lecture 37 Vladimir Kulyukin Computer Science Department Utah State University.
CS 206 Introduction to Computer Science II 11 / 10 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
CSE 780 Algorithms Advanced Algorithms Graph Algorithms Representations BFS.
Shortest Paths Definitions Single Source Algorithms
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CSE 780 Algorithms Advanced Algorithms SSSP Dijkstra’s algorithm SSSP in DAGs.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 09 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
All-Pairs Shortest Paths
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
How to navigate a maze? Can we go from s to t? Shortest route? How fast can we compute this?
Dijkstra's algorithm.
CS261 Data Structures DFS and BFS – Edge List Representation.
Shortest Path Algorithms. Kruskal’s Algorithm We construct a set of edges A satisfying the following invariant:  A is a subset of some MST We start with.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms: Minimum.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms Shortest-Path.
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
Search Related Algorithms. Graph Code Adjacency List Representation:
Graphs Data Structures and Algorithms A. G. Malamos Reference Algorithms, 2006, S. Dasgupta, C. H. Papadimitriou, and U. V. Vazirani Introduction to Algorithms,Third.
Data Structures and Algorithms Ver. 1.0 Session 17 Objectives In this session, you will learn to: Implement a graph Apply graphs to solve programming problems.
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.
Graphs David Kauchak cs302 Spring DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch.
D IJKSTRA ' S ALGORITHM. S INGLE -S OURCE S HORTEST P ATH P ROBLEM Single-Source Shortest Path Problem - The problem of finding shortest paths from a.
Shortest Path Graph Theory Basics Anil Kishore.
Minimum spanning trees (MST) Def: A spanning tree of a graph G is an acyclic subset of edges of G connecting all vertices in G. A sub-forest of G is an.
Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2010.
CSE 2331 / 5331 Topic 12: Shortest Path Basics Dijkstra Algorithm Relaxation Bellman-Ford Alg.
Nattee Niparnan. Dijkstra’s Algorithm Graph with Length.
COMP261 Lecture 6 Dijkstra’s Algorithm. Connectedness Is this graph connected or not? A Z FF C M N B Y BB S P DDGG AA R F G J L EE CC Q O V D T H W E.
D IJKSTRA ’ S S INGLE S OURCE S HORTEST P ATH Informatics Department Parahyangan Catholic University.
Graph Introduction, Searching Graph Theory Basics - Anil Kishore.
Graphs & Paths Presentation : Part II. Graph representation Given graph G = (V, E). May be either directed or undirected. Two common ways to represent.
Graphs + Shortest Paths David Kauchak cs302 Spring 2013.
Graph Searching CSIT 402 Data Structures II. 2 Graph Searching Methodology Depth-First Search (DFS) Depth-First Search (DFS) ›Searches down one path as.
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
GRAPH ALGORITHM. Graph A pair G = (V,E) – V = set of vertices (node) – E = set of edges (pairs of vertices) V = (1,2,3,4,5,6,7) E = ( (1,2),(2,3),(3,5),(1,4),(4,5),(6,7)
TIRGUL 10 Dijkstra’s algorithm Bellman-Ford Algorithm 1.
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
Nattee Niparnan. Graph  A pair G = (V,E)  V = set of vertices (node)  E = set of edges (pairs of vertices)  V = (1,2,3,4,5,6,7)  E = ((1,2),(2,3),(3,5),(1,4),(4,
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Lecture 13 Shortest Path.
Graph Algorithms BFS, DFS, Dijkstra’s.
Graph Representation (23.1/22.1)
Graph Algorithms: Shortest Path
CSE 417: Algorithms and Computational Complexity
Lecture 12 Shortest Path.
Lecture 14 Minimum Spanning Tree (cont’d)
Graphs: Shortest path and mst
Presentation transcript:

CSC 331: Algorithm Analysis Paths in Graphs

The DFS algorithm we gave is recursive. DFS generates a search tree showing paths from one vertex to all other connected vertices. We can re-write using a stack:

procedure explore( G, s ) Input: Graph G=(V, E) ; s ∈ V for all u in V : visited( u ) = false Stack = [s] (stack containing just s ) while Stack is not empty: u = pop( Stack ) visited( u ) = true for all edges (u, v) ∈ E : if visited( v ) = false : push( Stack, v )

Does DFS give the shortest path from S to all vertices? E D S C A B S C B AD E DFS generates a search tree showing paths from one vertex to all other connected vertices.

The distance between two vertices is the length of the shortest path between them.

E D S C A B S C B ADE A convenient way to compute distances from s to other vertices is to proceed layer by layer.

procedure bfs( G, s ) Input: Graph G=(V, E) ; s ∈ V Output: For all vertices u reachable from s, dist( u ) is set to the distance from s to u for all u in V : dist( u ) = ∞ dist( s ) = 0 Q = [s] (queue containing just s ) while Q is not empty: u = dequeue( Q ) for all edges (u, v) ∈ E : if dist( v ) = ∞ : enqueue( Q, v ) dist( v ) = dist( u ) + 1

E D S C A B S C B ADE What is the overall running time of BFS? O(|V| + |E|)

DFS makes deep incursions into a graph, retreating only when it runs out of new vertices to visit. BFS makes sure to visit vertices in increasing order of their distances from the starting point.

Suppose you are taking a train from San Francisco to Las Vegas, and want to find the quickest route

The weights on an edge can denote time, cost, or any other quantity that we would like to conserve. BFS finds shortest paths in any graph whose edges have unit length. Can we adapt it to a more general graph G = (V, E) whose edge lengths l e are positive integers?

Dijkstra’s algorithm is similar to BFS, except that it uses a priority queue to choose vertices in a way that takes the lengths into account.

D C A B E A: 0D: ∞ B: 4E: ∞ C: 2 Q = {C, B}

D C A B E A: 0D: 6 B: 3E: 7 C: 2 Q = {B, D, E}

D C A B E A: 0D: 5 B: 3E: 6 C: 2 Q = {D, E}

D C A B E A: 0D: 5 B: 3E: 6 C: 2 Q = {E}

D C A B E

procedure dijkstra(G, l, s) Input: Graph G=(V, E); positive edge lengths {l e : ∈ E}; vertex s in V Output: For all vertices u reachable from s, dist(u) is set to the distance from s to u for all u in V: dist(u) = ∞ prev(u) = nil dist(s) = 0 H = makequeue(V) (using dist-values as keys) while H is not empty: u = deletemin(H) for all edges (u,v) ∈ E: if dist(v) > dist(u) + l(u, v): dist(v) = dist(u) + l(u, v) prev(v) = u decreaseKey(H, v)

What is the running time for Dijkstra’s? O((|V| + |E|) log |V|) If the priority queue is implemented using a binary heap.