graph algorithm: breadth first search

25
GRAPH ALGORITHM: BREADTH FIRST SEARCH Nattee Niparnan

Upload: dudley

Post on 06-Jan-2016

31 views

Category:

Documents


1 download

DESCRIPTION

Nattee Niparnan. Graph Algorithm: Breadth First Search. 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. DFS and Length. DFS finds all nodes reachable from the starting node - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Graph Algorithm: Breadth First Search

GRAPH ALGORITHM:BREADTH FIRST SEARCH

Nattee Niparnan

Page 2: Graph Algorithm: Breadth First Search

Distance of nodes

The distance between two nodes is the length of the shortest path between them

SA 1

SC 1

SB 2

Page 3: Graph Algorithm: Breadth First Search

DFS and Length

DFS finds all nodes reachable from the starting node But it might not be “visited” according to

the distance

Page 4: Graph Algorithm: Breadth First Search

Ball and Strings

We can compute distance by proceeding from “layer” to “layer”

Page 5: Graph Algorithm: Breadth First Search

Shortest Path Problem (Undi, Unit) Input:

A graph, undirected A starting node S

Output: A label on every node, giving the

distance from S to that node

Page 6: Graph Algorithm: Breadth First Search

Breadth-First-Search Visit node according to its layer

procedure bfs(G; s)//Input: Graph G = (V,E), directed or undirected; vertex s V//Output: visit[u] is set to true for all nodes u reachable from vfor all u V : visit[u] = false

visit[s] = trueQueue Q = [s] (queue containing just s)while Q is not empty: u = eject(Q) previsit(u) visit[u] = true; for all edges (u,v) E: if visit[v] = false: visit[v] = true; inject(Q,v); postvisit(u)

Page 7: Graph Algorithm: Breadth First Search

Distance using BFSprocedure shortest_bfs(G, s)//Input: Graph G = (V,E), directed or undirected; vertex s V//Output: For all vertices u reachable from s, dist(u) is setto the distance from s to u.

for all u V : dist[u] = -1

dist[s] = 0Queue Q = [s] (queue containing just s)while Q is not empty: u = eject(Q); for all edges (u,v) E: if dist[v] = -1: inject(Q,v); dist[v] = dist[u] + 1;

Use dist as visit

Page 8: Graph Algorithm: Breadth First Search

DFS by Stackprocedure dfs(G, s)//Input: Graph G = (V,E), directed or undirected; vertex s V//Output: visit[u] is set to true for all nodes u reachable from v

for all u V : visit[u] = false

visit[s] = trueStack S = [s] (queue containing just s)while S is not empty: u = pop(S) previsit(u) for all edges (u,v) E: if visit [v] = false: push(S,v) visit[v] = true; postvisit(u)

Page 9: Graph Algorithm: Breadth First Search

DFS vs BFS

DFS goes depth first Trying to go further if possible Backtrack only when no other possible

way to go Using Stack

BFS goes breadth first Trying to visit node by the distance from

the starting node Using Queue

Page 10: Graph Algorithm: Breadth First Search

Dijkstra’s Algorithm

Graph with Length

Page 11: Graph Algorithm: Breadth First Search

Edge with Length

Length functionl(a,b) = distance from a to b

Page 12: Graph Algorithm: Breadth First Search

Finding Shortest Path

BFS can give us the shortest path Just convert the length edge into unit

edge

However, this is very slowImagine a case when the length is 1,000,000

Page 13: Graph Algorithm: Breadth First Search

Alarm Clock Analogy No need to walk to every

node Since it won’t change

anything We skip to the “actual”

node Set up the clock at alarm at

the target node

Page 14: Graph Algorithm: Breadth First Search

Alarm Clock Algorithm

Set an alarm clock for node s at time 0.

Repeat until there are no more alarms: Say the next alarm goes off at time T, for

node u. Then: The distance from s to u is T. For each neighbor v of u in G:

If there is no alarm yet for v, set one for time T + l(u, v).

If v's alarm is set for later than T + l(u, v), then reset it to this earlier time.

Page 15: Graph Algorithm: Breadth First Search

Dijkstra’s Algo from BFSprocedure dijkstra(G, l, s)//Input: Graph G = (V;E), directed or undirected; vertex s V; positive edge lengths l// Output: For all vertices u reachable from s, dist[u] is setto the distance from s to u.

for all u V : dist[u] = + prev(u) = nil

dist[s] = 0H = 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)

Page 16: Graph Algorithm: Breadth First Search

Another Implementation of Dijkstra’s Growing from Known Region of

shortest path Given a graph and a starting node s

What if we know a shortest path from s to some subset S’ V?

Divide and Conquer Approach?

Page 17: Graph Algorithm: Breadth First Search

Dijktra’s Algo #2procedure dijkstra(G, l, s)//Input: Graph G = (V;E), directed or undirected; vertex s V; positive edge lengths l// Output: For all vertices u reachable from s, dist[u] is setto the distance from s to u.

for all u V : dist[u] = + prev(u) = nil

dist[s] = 0

R = {} // (the “known region”)while R ≠ V : Pick the node v R with smallest dist[] Add v to R for all edges (v,z) E: if dist[z] > dist[v] + l(v,z): dist[z] = dist[v] + l(v,z)

Page 18: Graph Algorithm: Breadth First Search

Analysis

There are |V| ExtractMin Need to check all edges

At most |E|, if we use adjacency list Maybe |V2|, if we use adjacency matrix Value of dist[] might be changed

Depends on underlying data structure

Page 19: Graph Algorithm: Breadth First Search

Choice of DS

Using simple array Each ExtractMin uses O(V) Each change of dist[] uses O(1) Result = O(V2 + E) = O(V2)

Using binary heap Each ExtractMin uses O(lg V) Each change of dist[] uses O(lg V) Result = O( (V + E) lg V)

Can be O (V2 lg V)

Might be V2

Good when the graph is sparse

Page 20: Graph Algorithm: Breadth First Search

Fibonacci Heap

Using simple array Each ExtractMin uses O( lg V)

(amortized) Each change of dist[] uses O(1)

(amortized) Result = O(V lg V + E)

Page 21: Graph Algorithm: Breadth First Search

Graph with Negative Edge

Disjktra’s works because a shortest path to v must pass throught a node closer than v

Shortest path to A pass through B which is… in BFS sense… is further than A

Page 22: Graph Algorithm: Breadth First Search

Negative Cycle

A graph with a negative cycle has no shortest path The shortest.. makes no sense..

Hence, negative edge must be a directed

Page 23: Graph Algorithm: Breadth First Search

Key Idea in Shortest Path

Update the distance

if dist[z] > dist[v] + l(v,z): dist[z] = dist[v] + l(v,z)

This is safe to perform

now, a shortest path must has at most |V| - 1 edges

Page 24: Graph Algorithm: Breadth First Search

Bellman-Ford Algorithmprocedure BellmanFord(G, l, s)//Input: Graph G = (V;E), directed; vertex s V; edge lengths l (may be negative), no negative cycle// Output: For all vertices u reachable from s, dist[u] is setto the distance from s to u.

for all u V : dist[u] = + prev(u) = nil

dist[s] = 0

repeat |V| - 1 times: for all edges (a,b) E: if dist[b] > dist[a] + l(a,b): dist[b] = dist[a] + l(a,b)

Page 25: Graph Algorithm: Breadth First Search

Shortest Path in DAG

Path in DAG appears in linearized orderprocedure dag-shortest-path(G, l, s)

//Input: DAG G = (V;E), vertex s V; edge lengths l (may be negative)// Output: For all vertices u reachable from s, dist[u] is setto the distance from s to u.

for all u V : dist[u] = + prev(u) = nil

dist[s] = 0Linearize GFor each u V , in linearized order: for all edges (u,v) E: if dist[v] > dist[u] + l(u,v): dist[v] = dist[u] + l(y,v)