Graph Algorithms BFS, DFS, Dijkstra’s.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Lecture 12 CSE 331 Sep 30, Announcements Final exam: Dec 16, 11:45am-2:45pm, NSC 210 HW 2 solutions at the end of the lecture Mid term: Oct 16,
Lecture 24 CSE 331 Oct 30, Homework stuff Please turn in your HW 6 Graded HW 5 and HW 7 at the END of the lecture.
Lecture 27 CSE 331 Nov 3, Combining groups Groups can unofficially combine in the lectures.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
Lecture 25 CSE 331 Nov 2, Adding teeth to group talk Form groups of size at most six (6) Pick a group leader I will ask group leader(s) to come.
Lecture 13 CSE 331 Oct 2, Announcements Mid term in < 2 weeks Graded HW2 at the END of the class.
CSE 331: Review. Main Steps in Algorithm Design Problem Statement Algorithm Real world problem Problem Definition Precise mathematical def “Implementation”
Lecture 23 CSE 331 Oct 24, Reminder 2 points for Piazza participation 3 points for mini-project.
Lecture 12 CSE 331 Sep 28, Upcoming Important Dates Tuesday Oct 8 (~1.5 weeks) Mini Project group compositions Monday Oct 12 (2 weeks) Quiz 1 Monday.
Representing Graphs Depth First Search Breadth First Search Graph Searching Algorithms.
Introduction to Algorithms
COMP108 Algorithmic Foundations Greedy methods
CSE 2331/5331 Topic 9: Basic Graph Alg.
Lecture 12 Graph Algorithms
Lecture 23 CSE 331 Oct 26, 2016.
Lecture 15 CSE 331 Oct 5, 2012.
Lecture 15 CSE 331 Sep 29, 2014.
CSE 421: Introduction to Algorithms
Data Structures and Algorithms CMPSC 465
BFS,DFS Topological Sort
Lecture 24 CSE 331 Oct 28, 2016.
Lecture 24 CSE 331 Oct 27, 2017.
Lecture 14 CSE 331 Sep 30, 2016.
Lecture 12 CSE 331 Sep 26, 2016.
Lecture 12 CSE 331 Sep 25, 2017.
Lecture 14 CSE 331 Sep 30, 2011.
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
4.4 Shortest Paths in a Graph
Lecture 13 CSE 331 Oct 1, 2012.
Lecture 24 CSE 331 Oct 25, 2013.
Lecture 22 CSE 331 Oct 23, 2017.
Lecture 23 CSE 331 Oct 25, 2017.
Lecture 24 CSE 331 Oct 29, 2012.
Lecture 23 CSE 331 Oct 24, 2011.
Lecture 26 CSE 331 Nov 2, 2012.
Lecture 22 CSE 331 Oct 24, 2016.
Lecture 13 CSE 331 Sep 27, 2017.
Lecture 13 CSE 331 Sep 24, 2013.
Lecture 14 CSE 331 Oct 3, 2012.
Lecture 12 CSE 331 Sep 28, 2012.
Lecture 12 CSE 331 Sep 26, 2011.
Lecture 14 CSE 331 Sep 29, 2017.
Lecture 25 CSE 331 Oct 27, 2014.
Lecture 22 CSE 331 Oct 15, 2014.
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Lecture 14 Shortest Path (cont’d) Minimum Spanning Tree
CSE 373: Data Structures and Algorithms
Lecture 11 CSE 331 Sep 19, 2014.
Chapter 24: Single-Source Shortest Paths
Lecture 24 CSE 331 Oct 24, 2014.
Lecture 11 CSE 331 Sep 21, 2017.
Lecture 12 CSE 331 Sep 22, 2014.
GRAPHS G=<V,E> Adjacent vertices Undirected graph
Lecture 23 CSE 331 Oct 24, 2011.
Lecture 26 CSE 331 Nov 1, 2010.
Chapter 24: Single-Source Shortest Paths
CSE 417: Algorithms and Computational Complexity
Lecture 11 CSE 331 Sep 22, 2016.
Lecture 25 CSE 331 Oct 28, 2011.
Applications of BFS CSE 2011 Winter /17/2019 7:03 AM.
Lecture 15 CSE 331 Oct 4, 2010.
Single-Source Shortest Path & Minimum Spanning Trees
Lecture 13 Shortest Path (cont’d) Minimum Spanning Tree
Lecture 11 Graph Algorithms
Lecture 13 CSE 331 Sep 28, 2016.
Data Structures and Algorithm Analysis Lecture 8
More Graphs Lecture 19 CS2110 – Fall 2009.
Lecture 24 CSE 331 Oct 29, 2018.
Presentation transcript:

Graph Algorithms BFS, DFS, Dijkstra’s

Distance between u and v Length of the shortest length path between u and v Distance between and ? 3

Breadth First Search (BFS) Is s connected to t? Build layers of vertices connected to s Lj : all nodes at distance j from s L0 = {s} Assume L0,..,Lj have been constructed Lj+1 set of vertices not chosen yet but are connected to Lj Stop when new layer is empty

BFS Tree BFS naturally defines a tree rooted at s Add non-tree edges Lj forms the jth “level” in the tree u in Lj+1 is child of v in Lj from which it was “discovered” 1 7 1 L0 2 3 L1 2 3 8 4 5 4 5 7 8 L2 6 6 L3

DFS Data Structures queue<Node> 1 2 3 4 5 7 8 6 vector<bool> 1 2 3 4 5 6 7 8 1 L0 1 7 2 3 L1 2 3 8 4 5 4 5 7 8 L2 6 6 L3

DFS(u) Mark u as explored and add u to R For each edge (u,v) If v is not explored then DFS(v)

Depth First Search (DFS) http://xkcd.com/761/

Every non-tree edge is between a node and its ancestor A DFS run Every non-tree edge is between a node and its ancestor DFS(u) u is explored For every unexplored neighbor v of u DFS(v) 1 1 7 2 2 3 8 4 4 5 5 DFS tree 6 6 3 8 7

A DFS run using an explicit stack 7 8 1 7 7 6 3 2 3 5 8 4 4 5 5 3 6 2 3 1

http://xkcd.com/173/

Finding cycles in O(n+m) Run BFS or DFS If an edge is considered and the node is already explored That’s a cycle! 1 7 2 3 8 4 5 6

Shortest Path Problem http://xkcd.com/85/

Driving Directions

Shortest Path problem s 100 Input: Directed graph G=(V,E) w 15 5 s u w 100 Input: Directed graph G=(V,E) Edge lengths, le for e in E “start” vertex s in V 15 5 s u w 5 s u Output: All shortest paths from s to all nodes in V

Naïve Algorithm Ω(n!) time

Dijkstra’s shortest path algorithm E. W. Dijkstra (1930-2002)

Dijkstra’s shortest path algorithm 1 d’(w) = min e=(u,w) in E, u in R d(u)+le 1 2 4 3 y 4 3 u d(s) = 0 d(u) = 1 s x 2 4 d(w) = 2 d(x) = 2 d(y) = 3 d(z) = 4 w z 5 4 2 s w Input: Directed G=(V,E), le ≥ 0, s in V u R = {s}, d(s) =0 Shortest paths x While there is a x not in R with (u,x) in E, u in R z y Pick w that minimizes d’(w) Add w to R d(w) = d’(w)

Couple of remarks The Dijkstra’s algo does not explicitly compute the shortest paths Can maintain “shortest path tree” separately Dijkstra’s algorithm does not work with negative weights

Dijkstra’s shortest path algorithm (runtime) d’(v) = min e=(u,v) in E, u in S d(u)+le Input: Directed G=(V,E), le ≥ 0, s in V S = {s}, d(s) =0 While there is a v not in S with (u,v) in E, u in S At most n iterations Pick w that minimizes d’(w) Add w to S d(w) = d’(w) O(m) time O(mn) time bound is trivial O(m log n) time implementation is possible How?