6331 - Algorithms, CSE, OSU Elementary graph algorithms

48
6331 - Algorithms, CSE, OSU Elementary graph algorithms Instructor: Anastasios Sidiropoulos

Transcript of 6331 - Algorithms, CSE, OSU Elementary graph algorithms

Page 1: 6331 - Algorithms, CSE, OSU Elementary graph algorithms

6331 - Algorithms, CSE, OSUElementary graph algorithms

Instructor: Anastasios Sidiropoulos

Page 2: 6331 - Algorithms, CSE, OSU Elementary graph algorithms

Graph problems

I Many problems can be phrased as graph problems.

I Input: Graph G = (V ,E ).

I The running time is measured in terms of |V |, and |E |.

Page 3: 6331 - Algorithms, CSE, OSU Elementary graph algorithms

Graph problems

I Many problems can be phrased as graph problems.

I Input: Graph G = (V ,E ).

I The running time is measured in terms of |V |, and |E |.

Page 4: 6331 - Algorithms, CSE, OSU Elementary graph algorithms

Graph problems

I Many problems can be phrased as graph problems.

I Input: Graph G = (V ,E ).

I The running time is measured in terms of |V |, and |E |.

Page 5: 6331 - Algorithms, CSE, OSU Elementary graph algorithms

Representing a graph

Adjacency-matrix for a graph G = (V ,E ).

|V | × |V | matrix A = (aij), where

aij =

{1 if {i , j} ∈ E0 if {i , j} /∈ E

Storage space = Θ(|V |2).

Page 6: 6331 - Algorithms, CSE, OSU Elementary graph algorithms

Representing a graph

Adjacency-matrix for a graph G = (V ,E ).

|V | × |V | matrix A = (aij), where

aij =

{1 if {i , j} ∈ E0 if {i , j} /∈ E

Storage space = Θ(|V |2).

Page 7: 6331 - Algorithms, CSE, OSU Elementary graph algorithms

Representing a graph

The adjacency-list for a graph G = (V ,E ) is an array Adj of size|V |.

For each u ∈ V , Adj [u] is a list that contains all v ∈ V , with{u, v} ∈ E .

Storage space = Θ(|V |+ |E |).

Much smaller space when |E | � |V |2.

Page 8: 6331 - Algorithms, CSE, OSU Elementary graph algorithms

Representing a graph

The adjacency-list for a graph G = (V ,E ) is an array Adj of size|V |.

For each u ∈ V , Adj [u] is a list that contains all v ∈ V , with{u, v} ∈ E .

Storage space = Θ(|V |+ |E |).

Much smaller space when |E | � |V |2.

Page 9: 6331 - Algorithms, CSE, OSU Elementary graph algorithms

Representing a graph

The adjacency-list for a graph G = (V ,E ) is an array Adj of size|V |.

For each u ∈ V , Adj [u] is a list that contains all v ∈ V , with{u, v} ∈ E .

Storage space = Θ(|V |+ |E |).

Much smaller space when |E | � |V |2.

Page 10: 6331 - Algorithms, CSE, OSU Elementary graph algorithms

Representing a graph

The adjacency-list for a graph G = (V ,E ) is an array Adj of size|V |.

For each u ∈ V , Adj [u] is a list that contains all v ∈ V , with{u, v} ∈ E .

Storage space = Θ(|V |+ |E |).

Much smaller space when |E | � |V |2.

Page 11: 6331 - Algorithms, CSE, OSU Elementary graph algorithms

Breadth-first search

An algorithm for “exploring” a graph, starting from the givenvertex s.

Page 12: 6331 - Algorithms, CSE, OSU Elementary graph algorithms

Breadth-first searchBFS(G , s)

for each u ∈ G .V − {s}u.color = WHITEu.d =∞u.π = NIL

s.color = GRAYs.d = 0s.π = NILQ = ∅ENQUEUE(Q, s) //FIFO queuewhile Q 6= ∅

u = DEQUEUE(Q)for each v ∈ G .Adj [u]

if v .color = WHITEv .color = GRAYv .d = u.d + 1v .π = uENQUEUE(Q, v)

u.color = BLACK

Page 13: 6331 - Algorithms, CSE, OSU Elementary graph algorithms

Running time of BFS

I How many DEQUEUE operations?

A non-white vertex neverbecomes white. Every vertex is enqueued at most once. Atmost O(|V |) DEQUEUE operations.

I For every dequeued vertex u, we spend O(|G .Adj [u]|) time.Total length of all adjacency-lists is O(|E |).

I Total running time O(|V |+ |E |).

Page 14: 6331 - Algorithms, CSE, OSU Elementary graph algorithms

Running time of BFS

I How many DEQUEUE operations? A non-white vertex neverbecomes white.

Every vertex is enqueued at most once. Atmost O(|V |) DEQUEUE operations.

I For every dequeued vertex u, we spend O(|G .Adj [u]|) time.Total length of all adjacency-lists is O(|E |).

I Total running time O(|V |+ |E |).

Page 15: 6331 - Algorithms, CSE, OSU Elementary graph algorithms

Running time of BFS

I How many DEQUEUE operations? A non-white vertex neverbecomes white. Every vertex is enqueued at most once.

Atmost O(|V |) DEQUEUE operations.

I For every dequeued vertex u, we spend O(|G .Adj [u]|) time.Total length of all adjacency-lists is O(|E |).

I Total running time O(|V |+ |E |).

Page 16: 6331 - Algorithms, CSE, OSU Elementary graph algorithms

Running time of BFS

I How many DEQUEUE operations? A non-white vertex neverbecomes white. Every vertex is enqueued at most once. Atmost O(|V |) DEQUEUE operations.

I For every dequeued vertex u, we spend O(|G .Adj [u]|) time.Total length of all adjacency-lists is O(|E |).

I Total running time O(|V |+ |E |).

Page 17: 6331 - Algorithms, CSE, OSU Elementary graph algorithms

Running time of BFS

I How many DEQUEUE operations? A non-white vertex neverbecomes white. Every vertex is enqueued at most once. Atmost O(|V |) DEQUEUE operations.

I For every dequeued vertex u, we spend O(|G .Adj [u]|) time.

Total length of all adjacency-lists is O(|E |).

I Total running time O(|V |+ |E |).

Page 18: 6331 - Algorithms, CSE, OSU Elementary graph algorithms

Running time of BFS

I How many DEQUEUE operations? A non-white vertex neverbecomes white. Every vertex is enqueued at most once. Atmost O(|V |) DEQUEUE operations.

I For every dequeued vertex u, we spend O(|G .Adj [u]|) time.Total length of all adjacency-lists is O(|E |).

I Total running time O(|V |+ |E |).

Page 19: 6331 - Algorithms, CSE, OSU Elementary graph algorithms

Running time of BFS

I How many DEQUEUE operations? A non-white vertex neverbecomes white. Every vertex is enqueued at most once. Atmost O(|V |) DEQUEUE operations.

I For every dequeued vertex u, we spend O(|G .Adj [u]|) time.Total length of all adjacency-lists is O(|E |).

I Total running time O(|V |+ |E |).

Page 20: 6331 - Algorithms, CSE, OSU Elementary graph algorithms

Shortest paths

For u, v ∈ V , let δ(u, v) be the minimum number of edges in apath between u and v in G , and ∞ if no such path exists.

I.e., δ(u, v) is the shortest path distance between u and v in G .

A path between u and v in G of length δ(u, v) is called ashortest-path.

Page 21: 6331 - Algorithms, CSE, OSU Elementary graph algorithms

Shortest paths

For u, v ∈ V , let δ(u, v) be the minimum number of edges in apath between u and v in G , and ∞ if no such path exists.

I.e., δ(u, v) is the shortest path distance between u and v in G .

A path between u and v in G of length δ(u, v) is called ashortest-path.

Page 22: 6331 - Algorithms, CSE, OSU Elementary graph algorithms

Shortest paths

For u, v ∈ V , let δ(u, v) be the minimum number of edges in apath between u and v in G , and ∞ if no such path exists.

I.e., δ(u, v) is the shortest path distance between u and v in G .

A path between u and v in G of length δ(u, v) is called ashortest-path.

Page 23: 6331 - Algorithms, CSE, OSU Elementary graph algorithms

Analysis of BFS

LemmaFor any {u, v} ∈ E , we have

δ(s, v) ≤ δ(s, u) + 1.

Why?

Page 24: 6331 - Algorithms, CSE, OSU Elementary graph algorithms

Analysis of BFS

LemmaFor any {u, v} ∈ E , we have

δ(s, v) ≤ δ(s, u) + 1.

Why?

Page 25: 6331 - Algorithms, CSE, OSU Elementary graph algorithms

Analysis of BFS

LemmaAfter the termination of BFS, for each v ∈ V , we have

v .d ≥ δ(s, v).

Proof.Induction on the number of ENQUEUE operations.Inductive hypothesis: For all v ∈ V , we have v .d ≥ δ(s, v).Basis of the induction: s.d = 0, and v .d =∞ for all v 6= s.Consider some v ∈ G .Adj [u], immediately after dequeueing u.

v .d = u.d + 1

≥ δ(s, u) + 1

≥ δ(s, v) (by the previous Lemma)

Page 26: 6331 - Algorithms, CSE, OSU Elementary graph algorithms

Analysis of BFS

LemmaAfter the termination of BFS, for each v ∈ V , we have

v .d ≥ δ(s, v).

Proof.Induction on the number of ENQUEUE operations.

Inductive hypothesis: For all v ∈ V , we have v .d ≥ δ(s, v).Basis of the induction: s.d = 0, and v .d =∞ for all v 6= s.Consider some v ∈ G .Adj [u], immediately after dequeueing u.

v .d = u.d + 1

≥ δ(s, u) + 1

≥ δ(s, v) (by the previous Lemma)

Page 27: 6331 - Algorithms, CSE, OSU Elementary graph algorithms

Analysis of BFS

LemmaAfter the termination of BFS, for each v ∈ V , we have

v .d ≥ δ(s, v).

Proof.Induction on the number of ENQUEUE operations.Inductive hypothesis: For all v ∈ V , we have v .d ≥ δ(s, v).

Basis of the induction: s.d = 0, and v .d =∞ for all v 6= s.Consider some v ∈ G .Adj [u], immediately after dequeueing u.

v .d = u.d + 1

≥ δ(s, u) + 1

≥ δ(s, v) (by the previous Lemma)

Page 28: 6331 - Algorithms, CSE, OSU Elementary graph algorithms

Analysis of BFS

LemmaAfter the termination of BFS, for each v ∈ V , we have

v .d ≥ δ(s, v).

Proof.Induction on the number of ENQUEUE operations.Inductive hypothesis: For all v ∈ V , we have v .d ≥ δ(s, v).Basis of the induction: s.d = 0, and v .d =∞ for all v 6= s.

Consider some v ∈ G .Adj [u], immediately after dequeueing u.

v .d = u.d + 1

≥ δ(s, u) + 1

≥ δ(s, v) (by the previous Lemma)

Page 29: 6331 - Algorithms, CSE, OSU Elementary graph algorithms

Analysis of BFS

LemmaAfter the termination of BFS, for each v ∈ V , we have

v .d ≥ δ(s, v).

Proof.Induction on the number of ENQUEUE operations.Inductive hypothesis: For all v ∈ V , we have v .d ≥ δ(s, v).Basis of the induction: s.d = 0, and v .d =∞ for all v 6= s.Consider some v ∈ G .Adj [u], immediately after dequeueing u.

v .d = u.d + 1

≥ δ(s, u) + 1

≥ δ(s, v) (by the previous Lemma)

Page 30: 6331 - Algorithms, CSE, OSU Elementary graph algorithms

Analysis of BFS

LemmaAfter the termination of BFS, for each v ∈ V , we have

v .d ≥ δ(s, v).

Proof.Induction on the number of ENQUEUE operations.Inductive hypothesis: For all v ∈ V , we have v .d ≥ δ(s, v).Basis of the induction: s.d = 0, and v .d =∞ for all v 6= s.Consider some v ∈ G .Adj [u], immediately after dequeueing u.

v .d = u.d + 1

≥ δ(s, u) + 1

≥ δ(s, v) (by the previous Lemma)

Page 31: 6331 - Algorithms, CSE, OSU Elementary graph algorithms

Analysis of BFS

LemmaSuppose during the execution, Q = (v1, . . . , vr ), where v1 = head,vr = tail . Then for all i ∈ {1, . . . , r − 1}

vi .d ≤ vi+1.d ,

andvr .d ≤ v1.d + 1.

Why?

Page 32: 6331 - Algorithms, CSE, OSU Elementary graph algorithms

Analysis of BFS

LemmaSuppose during the execution, Q = (v1, . . . , vr ), where v1 = head,vr = tail . Then for all i ∈ {1, . . . , r − 1}

vi .d ≤ vi+1.d ,

andvr .d ≤ v1.d + 1.

Why?

Page 33: 6331 - Algorithms, CSE, OSU Elementary graph algorithms

Analysis of BFS

LemmaSuppose during the execution, both vi and vj are enqueued, and viis enqueued before vj . Then, vi .d ≤ vj .d when vj is enqueued.

Why?

Page 34: 6331 - Algorithms, CSE, OSU Elementary graph algorithms

Analysis of BFS

LemmaSuppose during the execution, both vi and vj are enqueued, and viis enqueued before vj . Then, vi .d ≤ vj .d when vj is enqueued.

Why?

Page 35: 6331 - Algorithms, CSE, OSU Elementary graph algorithms

Analysis of BFS

TheoremAfter termination, for all v ∈ V , we have

v .d = δ(s, v).

Moreover, for any v that is reachable from s, there exists ashortest path from s to v that consists of a shortest path from s tov .π, followed by the edge {v .π, v}.

Page 36: 6331 - Algorithms, CSE, OSU Elementary graph algorithms

Proof sketch

Suppose for the purpose of contradiction that there exists v withv .d 6= δ(s, v).

Pick such a v so that δ(s, v) is minimized.By the above Lemma, v .d > δ(s, v).Let u be the vertex preceding v in a shortest path from s to v . Wehave

v .d > δ(s, v) = δ(s, u) + 1 = u.d + 1.

Consider the time immediately after dequeueing u.

I If v is WHITE, then v .d = u.d + 1, a contradiction.

I If v is BLACK, then it is already dequeued, so by the aboveLemma v .d ≤ u.d , a contradiction.

I If v is GRAY, then it was painted GRAY after dequeueingsome vertex w , so v .d = w .d + 1 ≤ u.d + 1, a contradiction.

Page 37: 6331 - Algorithms, CSE, OSU Elementary graph algorithms

Proof sketch

Suppose for the purpose of contradiction that there exists v withv .d 6= δ(s, v).Pick such a v so that δ(s, v) is minimized.

By the above Lemma, v .d > δ(s, v).Let u be the vertex preceding v in a shortest path from s to v . Wehave

v .d > δ(s, v) = δ(s, u) + 1 = u.d + 1.

Consider the time immediately after dequeueing u.

I If v is WHITE, then v .d = u.d + 1, a contradiction.

I If v is BLACK, then it is already dequeued, so by the aboveLemma v .d ≤ u.d , a contradiction.

I If v is GRAY, then it was painted GRAY after dequeueingsome vertex w , so v .d = w .d + 1 ≤ u.d + 1, a contradiction.

Page 38: 6331 - Algorithms, CSE, OSU Elementary graph algorithms

Proof sketch

Suppose for the purpose of contradiction that there exists v withv .d 6= δ(s, v).Pick such a v so that δ(s, v) is minimized.By the above Lemma, v .d > δ(s, v).

Let u be the vertex preceding v in a shortest path from s to v . Wehave

v .d > δ(s, v) = δ(s, u) + 1 = u.d + 1.

Consider the time immediately after dequeueing u.

I If v is WHITE, then v .d = u.d + 1, a contradiction.

I If v is BLACK, then it is already dequeued, so by the aboveLemma v .d ≤ u.d , a contradiction.

I If v is GRAY, then it was painted GRAY after dequeueingsome vertex w , so v .d = w .d + 1 ≤ u.d + 1, a contradiction.

Page 39: 6331 - Algorithms, CSE, OSU Elementary graph algorithms

Proof sketch

Suppose for the purpose of contradiction that there exists v withv .d 6= δ(s, v).Pick such a v so that δ(s, v) is minimized.By the above Lemma, v .d > δ(s, v).Let u be the vertex preceding v in a shortest path from s to v . Wehave

v .d > δ(s, v) = δ(s, u) + 1 = u.d + 1.

Consider the time immediately after dequeueing u.

I If v is WHITE, then v .d = u.d + 1, a contradiction.

I If v is BLACK, then it is already dequeued, so by the aboveLemma v .d ≤ u.d , a contradiction.

I If v is GRAY, then it was painted GRAY after dequeueingsome vertex w , so v .d = w .d + 1 ≤ u.d + 1, a contradiction.

Page 40: 6331 - Algorithms, CSE, OSU Elementary graph algorithms

Proof sketch

Suppose for the purpose of contradiction that there exists v withv .d 6= δ(s, v).Pick such a v so that δ(s, v) is minimized.By the above Lemma, v .d > δ(s, v).Let u be the vertex preceding v in a shortest path from s to v . Wehave

v .d > δ(s, v) = δ(s, u) + 1 = u.d + 1.

Consider the time immediately after dequeueing u.

I If v is WHITE, then v .d = u.d + 1, a contradiction.

I If v is BLACK, then it is already dequeued, so by the aboveLemma v .d ≤ u.d , a contradiction.

I If v is GRAY, then it was painted GRAY after dequeueingsome vertex w , so v .d = w .d + 1 ≤ u.d + 1, a contradiction.

Page 41: 6331 - Algorithms, CSE, OSU Elementary graph algorithms

Proof sketch

Suppose for the purpose of contradiction that there exists v withv .d 6= δ(s, v).Pick such a v so that δ(s, v) is minimized.By the above Lemma, v .d > δ(s, v).Let u be the vertex preceding v in a shortest path from s to v . Wehave

v .d > δ(s, v) = δ(s, u) + 1 = u.d + 1.

Consider the time immediately after dequeueing u.

I If v is WHITE, then v .d = u.d + 1, a contradiction.

I If v is BLACK, then it is already dequeued, so by the aboveLemma v .d ≤ u.d , a contradiction.

I If v is GRAY, then it was painted GRAY after dequeueingsome vertex w , so v .d = w .d + 1 ≤ u.d + 1, a contradiction.

Page 42: 6331 - Algorithms, CSE, OSU Elementary graph algorithms

Proof sketch

Suppose for the purpose of contradiction that there exists v withv .d 6= δ(s, v).Pick such a v so that δ(s, v) is minimized.By the above Lemma, v .d > δ(s, v).Let u be the vertex preceding v in a shortest path from s to v . Wehave

v .d > δ(s, v) = δ(s, u) + 1 = u.d + 1.

Consider the time immediately after dequeueing u.

I If v is WHITE, then v .d = u.d + 1, a contradiction.

I If v is BLACK, then it is already dequeued, so by the aboveLemma v .d ≤ u.d , a contradiction.

I If v is GRAY, then it was painted GRAY after dequeueingsome vertex w , so v .d = w .d + 1 ≤ u.d + 1, a contradiction.

Page 43: 6331 - Algorithms, CSE, OSU Elementary graph algorithms

Proof sketch

Suppose for the purpose of contradiction that there exists v withv .d 6= δ(s, v).Pick such a v so that δ(s, v) is minimized.By the above Lemma, v .d > δ(s, v).Let u be the vertex preceding v in a shortest path from s to v . Wehave

v .d > δ(s, v) = δ(s, u) + 1 = u.d + 1.

Consider the time immediately after dequeueing u.

I If v is WHITE, then v .d = u.d + 1, a contradiction.

I If v is BLACK, then it is already dequeued, so by the aboveLemma v .d ≤ u.d , a contradiction.

I If v is GRAY, then it was painted GRAY after dequeueingsome vertex w , so v .d = w .d + 1 ≤ u.d + 1, a contradiction.

Page 44: 6331 - Algorithms, CSE, OSU Elementary graph algorithms

Proof sketch (cont.)

So, v .d = δ(s, v) for all v ∈ V .

For the last part of the theorem, if u = v .π, then v .d = u.d + 1.The assertion follows by induction.

Page 45: 6331 - Algorithms, CSE, OSU Elementary graph algorithms

Proof sketch (cont.)

So, v .d = δ(s, v) for all v ∈ V .

For the last part of the theorem, if u = v .π, then v .d = u.d + 1.The assertion follows by induction.

Page 46: 6331 - Algorithms, CSE, OSU Elementary graph algorithms

Breadth-first trees

We define the predecessor graph as Gπ = (Vπ,Eπ), where

Vπ = {v ∈ V : v .π 6= NIL} ∪ {s}

Eπ = {(v .π, v) : v ∈ Vs \ {s}}

Gπ is a breadth-first tree if Vπ consists of the vertices reachablefrom s and for all v ∈ Vπ, Gπ contains a unique simple path froms to v that is also a shortest path from s to v in G .

LemmaAfter the execution of BFS, the predecessor graph Gπ is abreadth-first tree.

Page 47: 6331 - Algorithms, CSE, OSU Elementary graph algorithms

Breadth-first trees

We define the predecessor graph as Gπ = (Vπ,Eπ), where

Vπ = {v ∈ V : v .π 6= NIL} ∪ {s}

Eπ = {(v .π, v) : v ∈ Vs \ {s}}

Gπ is a breadth-first tree if Vπ consists of the vertices reachablefrom s and for all v ∈ Vπ, Gπ contains a unique simple path froms to v that is also a shortest path from s to v in G .

LemmaAfter the execution of BFS, the predecessor graph Gπ is abreadth-first tree.

Page 48: 6331 - Algorithms, CSE, OSU Elementary graph algorithms

Breadth-first trees

We define the predecessor graph as Gπ = (Vπ,Eπ), where

Vπ = {v ∈ V : v .π 6= NIL} ∪ {s}

Eπ = {(v .π, v) : v ∈ Vs \ {s}}

Gπ is a breadth-first tree if Vπ consists of the vertices reachablefrom s and for all v ∈ Vπ, Gπ contains a unique simple path froms to v that is also a shortest path from s to v in G .

LemmaAfter the execution of BFS, the predecessor graph Gπ is abreadth-first tree.