BFS Dijkstra. Oct abhi shelat

Similar documents
Greedy Alg: Huffman abhi shelat

L feb abhi shelat

CS 4407 Algorithms Lecture: Shortest Path Algorithms

Analysis of Algorithms. Outline. Single Source Shortest Path. Andres Mendez-Vazquez. November 9, Notes. Notes

Breadth First Search, Dijkstra s Algorithm for Shortest Paths

Introduction to Algorithms

Introduction to Algorithms

Design and Analysis of Algorithms

Single Source Shortest Paths

CMPS 2200 Fall Carola Wenk Slides courtesy of Charles Leiserson with small changes by Carola Wenk. 10/8/12 CMPS 2200 Intro.

Introduction to Algorithms

CS 253: Algorithms. Chapter 24. Shortest Paths. Credit: Dr. George Bebis

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

Algorithm Design and Analysis

Single Source Shortest Paths

Introduction to Algorithms

CMPS 6610 Fall 2018 Shortest Paths Carola Wenk

Slides credited from Hsueh-I Lu & Hsu-Chun Hsiao

Algorithms Booklet. 1 Graph Searching. 1.1 Breadth First Search

Algorithm Design and Analysis

abhi shelat

Shortest Path Algorithms

Dijkstra s Single Source Shortest Path Algorithm. Andreas Klappenecker

CS 6301 PROGRAMMING AND DATA STRUCTURE II Dept of CSE/IT UNIT V GRAPHS

Shortest Paths. CS 320, Fall Dr. Geri Georg, Instructor 320 ShortestPaths 3

Discrete Wiskunde II. Lecture 5: Shortest Paths & Spanning Trees

Assignment 5: Solutions

CSE 591 Foundations of Algorithms Homework 4 Sample Solution Outlines. Problem 1

25. Minimum Spanning Trees

25. Minimum Spanning Trees

Query Processing in Spatial Network Databases

Breadth-First Search of Graphs

Introduction to Algorithms

Another way of saying this is that amortized analysis guarantees the average case performance of each operation in the worst case.

CSE 4502/5717 Big Data Analytics Spring 2018; Homework 1 Solutions

A faster algorithm for the single source shortest path problem with few distinct positive lengths

Data Structures and and Algorithm Xiaoqing Zheng

Lecture Notes for Chapter 25: All-Pairs Shortest Paths

Graph Search Howie Choset

CS213d Data Structures and Algorithms

Algorithms: Lecture 12. Chalmers University of Technology

Design and Analysis of Algorithms

Discrete Optimization 2010 Lecture 2 Matroids & Shortest Paths

Divide-and-Conquer Algorithms Part Two

CSCE 750 Final Exam Answer Key Wednesday December 7, 2005

Lecture Notes Discrete Optimization

CS 161: Design and Analysis of Algorithms

Using preprocessing to speed up Brandes betweenness centrality algorithm

Examination paper for TDT4120 Algorithms and Data Structures

Discrete Optimization 2010 Lecture 3 Maximum Flows

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Chapter 5 Data Structures Algorithm Theory WS 2017/18 Fabian Kuhn

Part IA Algorithms Notes

CMU Lecture 4: Informed Search. Teacher: Gianni A. Di Caro

Ngày 20 tháng 7 năm Discrete Optimization Graphs

8 Priority Queues. 8 Priority Queues. Prim s Minimum Spanning Tree Algorithm. Dijkstra s Shortest Path Algorithm

CSE548, AMS542: Analysis of Algorithms, Fall 2017 Date: Oct 26. Homework #2. ( Due: Nov 8 )

Fibonacci Heaps These lecture slides are adapted from CLRS, Chapter 20.

2-INF-237 Vybrané partie z dátových štruktúr 2-INF-237 Selected Topics in Data Structures

A Simple Implementation Technique for Priority Search Queues

Discrete Mathematics, Spring 2004 Homework 9 Sample Solutions

CS 410/584, Algorithm Design & Analysis, Lecture Notes 4

Greedy Algorithms. CSE 101: Design and Analysis of Algorithms Lecture 10

COMP251: Bipartite graphs

Algorithm Design and Analysis

Priority queues implemented via heaps

ENS Lyon Camp. Day 2. Basic group. Cartesian Tree. 26 October

Lecture 11. Single-Source Shortest Paths All-Pairs Shortest Paths

CPSC 320 Sample Final Examination December 2013

CS781 Lecture 3 January 27, 2011

IS 709/809: Computational Methods in IS Research Fall Exam Review

Matching Residents to Hospitals

Informed Search. Chap. 4. Breadth First. O(Min(N,B L )) BFS. Search. have same cost BIBFS. Bi- Direction. O(Min(N,2B L/2 )) BFS. have same cost UCS

Algorithms Theory. 08 Fibonacci Heaps

Maximum Flow. Reading: CLRS Chapter 26. CSE 6331 Algorithms Steve Lai

MA015: Graph Algorithms

Informed Search. Day 3 of Search. Chap. 4, Russel & Norvig. Material in part from

Slides for CIS 675. Huffman Encoding, 1. Huffman Encoding, 2. Huffman Encoding, 3. Encoding 1. DPV Chapter 5, Part 2. Encoding 2

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

ne a priority queue for the nodes of G; ile (! PQ.is empty( )) select u PQ with d(u) minimal and remove it; I Informatik 1 Kurt Mehlhorn

Mon Tue Wed Thurs Fri

Dynamic Programming: Shortest Paths and DFA to Reg Exps

Myriad of applications

Algorithms and Theory of Computation. Lecture 13: Linear Programming (2)

ICS 252 Introduction to Computer Design

Dynamic Programming: Shortest Paths and DFA to Reg Exps

Greedy Algorithms. CSE 101: Design and Analysis of Algorithms Lecture 9

Lecture XIV MINIMUM COST PATHS

Contents Lecture 4. Greedy graph algorithms Dijkstra s algorithm Prim s algorithm Kruskal s algorithm Union-find data structure with path compression

CMPSCI 611: Advanced Algorithms

Shortest paths with negative lengths

Objec&ves. Review. Data structure: Heaps Data structure: Graphs. What is a priority queue? What is a heap?

Preliminaries. Graphs. E : set of edges (arcs) (Undirected) Graph : (i, j) = (j, i) (edges) V = {1, 2, 3, 4, 5}, E = {(1, 3), (3, 2), (2, 4)}

Information Theory and Statistics Lecture 2: Source coding

Heaps and Priority Queues

Algorithms and Data Structures (COMP 251) Midterm Solutions

CSE 431/531: Analysis of Algorithms. Dynamic Programming. Lecturer: Shi Li. Department of Computer Science and Engineering University at Buffalo

Each internal node v with d(v) children stores d 1 keys. k i 1 < key in i-th sub-tree k i, where we use k 0 = and k d =.

CSE548, AMS542: Analysis of Algorithms, Fall 2016 Date: Nov 30. Final In-Class Exam. ( 7:05 PM 8:20 PM : 75 Minutes )

Partha Sarathi Mandal

Transcription:

4102 BFS Dijkstra Oct 22 2009 abhi shelat

breadth first search

bfs(g, a) 1 2 a b 1 2 d c e f g 2 h

bfs theorem

Theorem 1 (CLRS, p. 599) Let G =(V, E) be a graph and suppose that BFS is run on G from vertex s V. Then, when BFS terminates, d v = δ(s, v) for all v V where δ(s, v) = if there is no path from s to v in G.

claim:

lemma: when bfs terminates,

lemma: then suppose is enqueued before during bfs

breadth first search

Theorem 1 (CLRS, p. 599) Let G =(V, E) be a graph and suppose that BFS is run on G from vertex s V. Then, when BFS terminates, d v = δ(s, v) for all v V where δ(s, v) = if there is no path from s to v in G.

breadth first search

dfsall(g =(V, E)) 1 for each v V set d v 2 Set t 0 and S while s V such that d s = 4 do push(s, s) 5 while S is not empty 6 do u pop(s) 7 if d u = or u=s 8 then 9 t t + 1 10 d u t 11 for each v Adj(u) such that d v = 12 do push(s, v)

dfs(g, a)

partial order

http://scienceblogs.com/goodmath/2007/10/making_graph_algorithms_fast_u.php

http://scienceblogs.com/goodmath/2007/10/making_graph_algorithms_fast_u.php

strongly connected component definition: maximal set of vertices such that for every pair (a) u is connected to v (b) v is connected to u

example

dfs allows one to compute the strongly connected component of a graph.

unit edge weight 1 2 1 2 2

shortest paths b 8 d 8 g 10 2 a 9 e 5 9 i 12 11 c 1 f 6 h image:www.princegeorgeva.org, thefranciscofamily.org, www.rightdriveacademy.co.uk, www.ccscambridge.org, www.drawingcoach.com, www.pastoral.org.uk, www.daasgallery.com

bfs b 8 d 8 g 7 2 9 e 5 9 i 11 a 12 c 1 f 6 h

b a c

b a c

b a c

shortest paths b 8 d 8 g 10 2 a 9 e 5 9 i 12 11 c 1 f 6 h image:www.princegeorgeva.org, thefranciscofamily.org, www.rightdriveacademy.co.uk, www.ccscambridge.org, www.drawingcoach.com, www.pastoral.org.uk, www.daasgallery.com

shortest paths b 8 d 8 g 10 2 a 9 e 5 9 i 12 11 c 1 f 6 h alarmclock model image:www.princegeorgeva.org, thefranciscofamily.org, www.rightdriveacademy.co.uk, www.ccscambridge.org, www.drawingcoach.com, www.pastoral.org.uk, www.daasgallery.com

new data structure

binary heap full tree, key value <= to key of children

binary heap full tree, key value <= to key of children 10 5 11 1 6 9 2 44

binary heap full tree, key value <= to key of children 10 5 11 1 6 9 2 44 8

binary heap full tree, key value <= to key of children 10 5 11 8 6 9 2 44 1

binary heap full tree, key value <= to key of children how to extractmin? 8 5 11 10 6 9 2 44 1

binary heap full tree, key value <= to key of children how to extractmin? 1 8 5 11 10 6 9 2 44 1

binary heap full tree, key value <= to key of children how to extractmin? 5 8 1 11 10 6 9 2 44 1

binary heap full tree, key value <= to key of children how to extractmin? 5 8 6 11 10 1 9 2 44 1

binary heap full tree, key value <= to key of children how to decreasekey? 5 8 6 11 10 1 9 2 44

binary heap full tree, key value <= to key of children how to decreasekey? 5 7 6 11 8 1 9 2 44

implementation use a priority queue to keep track of light edges insert: makequeue: extractmin: decreasekey:

@/!(=, 1, ))!"# "%42 % = {)} $" >A%B >A)B C < 9DE:9:9(<, )) %&'() < = $" % F9E:9:9(<)!"# "%42 v 7>?A%B $" '! >AvB = *&)+ >AvB >A%B + 5 9DE:9:9(<,v)

algorithm

Dijkstra(G =(V,E),s) 1 for all v V 2 do d u π u nil 4 d s 0 5 Q makequeue(v ) use d u as key 6 while Q = 7 do u extractmin(q) 8 for each v Adj (u) 9 do if d v >d u + w(u, v) 10 then d v d u + w(u, v) 11 π v u 12 decreasekey(q, v)

@/!(=, 1, ))!"# "%42 % = {)} $" >A%B >A)B C < 9DE:9:9(<, )) %&'() < = $" % F9E:9:9(<)!"# "%42 v 7>?A%B $" '! >AvB = *&)+ >AvB >A%B + 5 9DE:9:9(<,v) Dijkstra(G =(V,E),s) 1 for all v V 2 do d u π u nil 4 d s 0 5 Q makequeue(v ) use d u as key 6 while Q = 7 do u extractmin(q) 8 for each v Adj (u) 9 do if d v >d u + w(u, v) 10 then d v d u + w(u, v) 11 π v u 12 decreasekey(q, v)

b 8 d 8 g 10 7 2 a 9 e 5 9 i 12 11 c 1 f 6 h

b 8 d 8 g 10 7 2 a 9 0 e 5 9 i 12 11 c 1 f 6 h

b 10 8 d 8 g 10 7 2 a 9 0 e 5 9 i 12 11 c 1 12 f 6 h

b 10 8 d 18 8 g 10 7 2 a 9 0 e 5 9 i 12 11 c 1 12 f 6 h

b 10 8 d 18 8 g 10 7 2 a 9 0 e 15 5 9 i 12 11 c 1 12 f 1 6 h

b 10 8 d 18 8 g 10 7 2 a 9 0 e 15 5 9 i 12 11 c 1 12 f 6 1 19 h

b 10 8 d 18 8 g 26 10 7 2 a 9 0 e 15 5 9 i 12 11 c 1 12 f 6 1 19 h

b 10 8 d 18 8 g 26 10 7 2 a 9 0 e 15 5 9 i 0 12 11 c 1 12 f 6 1 19 h

b 10 8 d 18 8 g 26 10 7 2 a 9 0 e 15 5 9 i 0 28 12 11 c 1 12 f 6 1 19 h

running time Dijkstra(G =(V,E),s) 1 for all v V 2 do d u π u nil 4 d s 0 5 Q makequeue(v ) use d u as key 6 while Q = 7 do u extractmin(q) 8 for each v Adj (u) 9 do if d v >d u + w(u, v) 10 then d v d u + w(u, v) 11 π v u 12 decreasekey(q, v)

why does dijkstra work?

steps along the way triangle inequality: (u, v) E, δ(s, v) δ(s, u)+w(u, v) upper bound: d v δ(s, v) no paths: δ(s, v) = = d v =