Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Exploring Graphs: BFS, DFS, and Shortest Paths, Study notes of Computer Science

The concepts of breadth-first search (bfs) and depth-first search (dfs) algorithms, their differences, and the analysis of their time complexity. Additionally, it discusses the shortest paths problem and its solution using dijkstra's algorithm.

Typology: Study notes

Pre 2010

Uploaded on 08/18/2009

koofers-user-4ce
koofers-user-4ce šŸ‡ŗšŸ‡ø

10 documents

1 / 10

Toggle sidebar

Related documents


Partial preview of the text

Download Exploring Graphs: BFS, DFS, and Shortest Paths and more Study notes Computer Science in PDF only on Docsity!

CS 362, Lecture 19

Jared Saia

University of New Mexico

Todayā€™s Outline

ā€¢

BFS and DFS Wrapup

ā€¢

Shortest Paths

Generic Traverse

Traverse(s){

while (the bag is not empty){put (nil,s) in bag;

if (v is unmarked)take some edge (p,v) from the bag

for each edge (v,w) incident to v{parent(v) = p;mark v;

put (v,w) into the bag;

}

}

}

}

DFS and BFS

ā€¢

If we implement the ā€œbagā€ by using a stack, we have

Depth

First Search

ā€¢

If we implement the ā€œbagā€ by using a queue, we have

Breadth

First Search

Analysis

ā€¢

we implement the bag)for the ā€œforā€ loop is only a constant per edge (no matter howNote that if we use adjacency lists for the graph, the overhead

ā€¢

operation on the bag takes constant timeIf we implement the bag using either stacks or queues, each

ā€¢

Hence the overall runtime is

O

(

|

V

|

+

|

E

|) =

O

(

|

E

|

)

DFS vs BFS

ā€¢

trees are short and fatNote that DFS trees tend to be long and skinny while BFS

ā€¢

In addition, the BFS tree contains

shortest paths

from the

start vertex

s

to every other vertex in its connected compo-

of edges in the path)nent. (here we define the length of a path to be the number

Final Note

ā€¢

Now assume the edges are weighted

ā€¢

If we implement the ā€œbagā€ using a

priority queue

,

always

have a version of Primā€™s algorithmextracting the minimum weight edge from the bag, then we

ā€¢

Each extraction from the ā€œbagā€ now takes

O

(

|

E

|

) time so

the total running time is

O

(

|

V

|

+

|

E

|

(^) log

|

E

|

)

Example

a

b

e^ d

f

c

a

b

e d

f

c

of one component of the example graph, with start vertex A depth-first spanning tree and a breadth-first spanning tree

a .

Searching Disconnected Graphs

the connected component of the start vertex If the graph is disconnected, then Traverse only visits nodes in

s .

If we want to

visit

all

vertices,

we

can

use

the

following

ā€œwrapperā€

around

TraverseAll(){Traverse

for all vertices v{

if (v is unmarked){

Traverse(v);

}

}

}

DFS and BFS

ā€¢

and directed graphsNote that we can do DFS and BFS equally well on undirected

ā€¢

If the graph is undirected, there are two types of edges in

G

:

not in this treeedges that are in the DFS or BFS tree and edges that are

ā€¢

If the graph is directed, there are several types of edges

DFS in Directed Graphs

ā€¢

Tree edges

are edges that are in the tree itself

ā€¢

Back edges

are those edges (

u, v

) connecting a vertex

u

to

an ancestor

v

in the DFS tree

ā€¢

Forward edges

are nontree edges (

u, v

) that connect a vertex

u

to a descendant in a DFS tree

ā€¢

Cross edges

are all other edges.

They go between two ver-

tices where neither vertex is a descendant of the other

Acyclic graphs

ā€¢

Useful Fact:

A directed graph

G

is acyclic if and only if a

DFS of

G

yeilds no back edges

ā€¢

Challenge: Try to prove this fact.

Take Away

ā€¢

BFS and DFS are two useful algorithms for exploring graphs

ā€¢

algorithm.Each of these algorithms is an instantiation of the Traverse

BFS uses a queue to hold the edges and DFS

uses a stack

ā€¢

the nodes which are reachable from the start nodeEach of these algorithms constructs a spanning tree of all

s

Shortest Paths Problem

ā€¢

Another

interesting

problem

for

graphs

is

that

of

finding

shortest paths

ā€¢

Assume we are given a weighted

directed

graph

G

= (

V, E

)

with two special vertices, a source

s

and a target

t

ā€¢

We want to find the shortest directed path from

s

to

t

ā€¢

In other words, we want to find the path

p

starting at

s

and

ending at

t

minimizing the function

w

( p ) =

e āˆ‘ āˆˆ p w

( e )

Example

ā€¢

querque,NM to Seattle,WAImagine we want to find the fastest way to drive from Albu-

ā€¢

roads, weights are driving times,We might use a graph whose vertices are cities, edges are

s

is Albuquerque and

t

is

Seattle

ā€¢

road might be different in different directions (e.g.The graph is directed since driving times along the same

because

of construction, speed traps, etc)

SSSP

ā€¢

the following more generalEvery algorithm known for solving this problem actually solves

single source shortest paths

or

SSSP problem:

ā€¢

Find the shortest path from the source vertex

s

to

every

other vertex in the graph

ā€¢

This problem is usually solved by finding a

shortest path tree

rooted at

s

that contains all the desired shortest paths

Shortest Path Tree

ā€¢

then they form a treeItā€™s not hard to see that if the shortest paths are unique,

ā€¢

shortest paths are themselves shortest pathsTo prove this, we need only observe that the sub-paths of

ā€¢

paths is a treecan always choose just one of them, so that the union of theIf there are multiple shotest paths to the same vertex, we

ā€¢

If there are shortest paths to two vertices

u

and

v

which

of the paths so that the two paths diverge once only.diverge, then meet, then diverge again, we can modify one

Example

s

v u

a

b

c

d

x

y

If

s ā†’ a ā†’ b ā†’ c ā†’ d ā†’ v

and

s ā†’ a ā†’ x ā†’ y ā†’ d ā†’ u

are both

shortest paths,

then

s

ā†’

a

ā†’

b (^) ā†’

c ā†’

d

ā†’

u

is also a shortest path.

MST vs SPT

ā€¢

can be differentNote that the minimum spanning tree and shortest path tree

ā€¢

multiple shortest path trees (one for every source vertex)For one thing there may be only one MST but there can be

Example

8

5

10

2

3

18

16

12

14

30

4

26

8

5

10

2

3

18

16

12

14

30

4

26

A minimum spanning tree (left) and a shortest path tree rooted at the

topmost vertex (right).

Negative Weights

ā€¢

Weā€™ll actually allow negative weights on edges

ā€¢

no shortest pathThe presence of a negative cycle might mean that there is

ā€¢

A shortest path from

s

to

t

exists if and only if there is

at

least one

path from

s

to

t

but no path from

s

to

t

that

touches a negative cycle

ā€¢

In the following example, there is no shortest path from

s

to

t

s

t

5

(^2)

āˆ’

(^4)

1

3

SSSP Algorithms

ā€¢

graphs.Weā€™ll now go over some algorithms for SSSP on directed

ā€¢

modificationThese algorithms will work for undirected graphs with slight

ā€¢

and forth across the same undirected negative-weight edgeIn particular, we must specifically prohibit alternating back

ā€¢

cial cases of a single generic algorithmLike for graph traversal, all the SSSP algorithms will be spe-

SSSP Algorithms

Each vertex

v

in the graph will store two values which describe

a

tentative

shortest path from

s

to

v

ā€¢

dist

(

v ) is the length of the tentative shortest path between

s

and

v

ā€¢

pred

(

v ) is the predecessor of

v

in this tentative shortest path

ā€¢

The

predecessor

pointers

automatically

define

a

tentative

shortest path tree

Defns

Initially we set:

dist

(

s ) = 0,

pred

(

s ) =

N U LL

ā€¢

For every vertex

v

6

s ,

dist

(

v ) =

āˆž

and

pred

(

v ) =

N U LL

Relaxation

ā€¢

We call an edge (

u, v

)

tense

if

dist

(

u ) +

(^) w

( u, v

)

< dist

(

v )

ā€¢

If (

u, v

) is tense, then the tentative shortest path from

s

to

v

is incorrect since the path

s

to

u

and then (

u, v

) is shorter

ā€¢

graph andOur generic algorithm repeatedly finds a tense edge in the

relaxes

it

ā€¢

have our desired shortest path treeIf there are no tense edges, our algorithm is finished and we

Relax

Relax(u,v){

pred(v) = u;dist(v) = dist(u) + w(u,v);

}

Correctness

ā€¢

from three simple claimsThe correctness of the relaxation algorithm follows directly

ā€¢

we make choices about which edges to relaxThe run time of the algorithm will depend on the way that

Claim 1

ā€¢

If

dist

(

v )

6

āˆž

, then

dist

(

v ) is the total weight of the prede-

cessor chain ending at

v :

s

ā†’ Ā· Ā· Ā· ā†’

pred

(

pred

(

v ))

ā†’

pred

(

v )

ā†’

(^) v.

ā€¢

in the path fromThis is easy to prove by induction on the number of edges

s

to

v

. (left as an exercise)

Claim 2

ā€¢

If the algorithm halts, then

dist

(

v )

ā‰¤

w

( s

;

v ) for

any

path

s

;

v .

ā€¢

in the pathThis is easy to prove by induction on the number of edges

s

;

(^) v

. (which you will do in the hw)

Claim 3

ā€¢

reachable fromThe algorithm halts if and only if there is no negative cycle

s .

ā€¢

cyclecycle, then after the first edge in the cycle is relaxed, theThe ā€˜only ifā€™ direction is easyā€”if there is a reachable negative

always

has at least one tense edge.

ā€¢

step reduces either the number of vertices withThe ā€˜ifā€™ direction follows from the fact that every relaxation

dist

(

v ) =

āˆž

by some positive amount. by 1 or reduces the sum of the finite shortest path lengths

Generic SSSP

ā€¢

or what order to relax them inWe havenā€™t yet said how to detect which edges can be relaxed

ā€¢

tionsThe following Generic SSSP algorithm answers these ques-

ā€¢

the source vertexWe will maintain a ā€œbagā€ of vertices initially containing just

s

ā€¢

Whenever we take a vertex

u

out of the bag, we scan all of

its outgoing edges, looking for something to relax

ā€¢

Whenever we successfully relax an edge (

u, v

), we put

v

in

the bag

InitSSSP

InitSSSP(s){

for all vertices v != s{pred(s) = NULL;dist(s) = 0;

pred(v) = NULL;dist(v) = infinity;

}

}

GenericSSSP

GenericSSSP(s){

while the bag is not empty{put s in the bag;InitSSSP(s);

for all edges (u,v){take u from the bag;

if (u,v) is tense{

put v in the bag;Relax(u,v);

}

}

}

}

Generic SSSP

ā€¢

for the bag gives us different algorithmsJust as with graph traversal, using different data structures

ā€¢

Some obvious choices are: a stack, a queue and a heap

ā€¢

Unfortunately if we use a stack, we need to perform Ī˜(

|E

| )

student)relaxation steps in the worst case (an exercise for the diligent

ā€¢

The other possibilities are more efficient

Diskstraā€™s Algorithm

ā€¢

v If we implement the bag as a heap, where the key of a vertex

is

dist

(

v ), we obtain Dijkstraā€™s algorithm

ā€¢

negative-weight edgesDijkstraā€™s algorithm does particularly well if the graph has no

ā€¢

thatIn this case, itā€™s not hard to show (by induction, of course)

the

vertices

are

scanned

in

increasing

order

of

their

shortest-path distance from

s

ā€¢

that each edge is relaxed at most onceIt follows that each vertex is scanned at most once, and thus

Dijktraā€™s Algorithm

ā€¢

tance fromSince the key of each vertex in the heap is its tentative dis-

s , the algorithm performs a DecreaseKey opera-

tion every time an edge is relaxed

ā€¢

Thus the algorithm performs at most

|

E

|

DecreaseKeyā€™s

ā€¢

Similarly, there are at most

|

V

|

Insert and ExtractMin oper-

ations

ā€¢

running time of Dijkstraā€™s algorithm isThus if we store the vertices in a Fibonacci heap, the total

O

(

|

E

|

+

|

V

|

(^) log

|

V

|

)

Negative Edges

ā€¢

This analysis assumes that no edge has negative weight

ā€¢

tialweight edges but the worst-case run time could be exponen-The algorithm given here is still correct if there are negative

ā€¢

graphs with negative edges (which they make clear)The algorithm in our text book gives incorrect results for

Example

1

(^3)

2

(^0)

5

10

12

8

4

(^6)

(^37)

s

āˆž āˆž

āˆž

āˆž

4

3

1

(^3)

2

(^0)

5

10

12

8

4

(^6)

(^37)

s 0

āˆž āˆž

āˆž

āˆž āˆž

āˆž

1

(^3)

2

(^0)

5

10

12

8

4

(^6)

(^37)

s

āˆž āˆž

4 āˆž

3 12

1

(^3)

2

(^0)

5

10

12

8

4

(^6)

(^37)

s

āˆž

4 āˆž

3 9

4

1

(^3)

2

(^0)

5

10

12

8

4

(^6)

(^37)

s 4

3 9

4

7

14

(^0)

0

(^0)

0

At each phase, the shaded vertices are in the heap, and the bold vertex has Four phases of Dijkstraā€™s algorithm run on a graph with no negative edges.

just been scanned.

The bold edges describe the evolving shortest path tree.