166
Analysis of Algorithms Basic Graph Algorithms Andres Mendez-Vazquez October 28, 2015 1 / 84

18 Basic Graph Algorithms

Embed Size (px)

Citation preview

Page 1: 18 Basic Graph Algorithms

Analysis of AlgorithmsBasic Graph Algorithms

Andres Mendez-Vazquez

October 28, 2015

1 / 84

Page 2: 18 Basic Graph Algorithms

Outline

1 IntroductionGraphs EverywhereHistoryBasic TheoryRepresenting Graphs in a Computer

2 Traversing the GraphBreadth-first searchDepth-First Search

3 ApplicationsFinding a path between nodesConnected ComponentsSpanning TreesTopological Sorting

2 / 84

Page 3: 18 Basic Graph Algorithms

Outline

1 IntroductionGraphs EverywhereHistoryBasic TheoryRepresenting Graphs in a Computer

2 Traversing the GraphBreadth-first searchDepth-First Search

3 ApplicationsFinding a path between nodesConnected ComponentsSpanning TreesTopological Sorting

3 / 84

Page 4: 18 Basic Graph Algorithms

We are full of Graphs

Maps

4 / 84

Page 5: 18 Basic Graph Algorithms

We are full of Graphs

Branch CPU estimators

5 / 84

Page 6: 18 Basic Graph Algorithms

We are full of Graphs

Social Networks

6 / 84

Page 7: 18 Basic Graph Algorithms

Outline

1 IntroductionGraphs EverywhereHistoryBasic TheoryRepresenting Graphs in a Computer

2 Traversing the GraphBreadth-first searchDepth-First Search

3 ApplicationsFinding a path between nodesConnected ComponentsSpanning TreesTopological Sorting

7 / 84

Page 8: 18 Basic Graph Algorithms

History

Something NotableGraph theory started with Euler who was asked to find a nice path acrossthe seven Königsberg bridges

The Actual City

8 / 84

Page 9: 18 Basic Graph Algorithms

HistorySomething NotableGraph theory started with Euler who was asked to find a nice path acrossthe seven Königsberg bridges

The Actual City

8 / 84

Page 10: 18 Basic Graph Algorithms

No solution for a odd number of Bridges

What we wantThe (Eulerian) path should cross over each of the seven bridges exactlyonce

We cannot do this for the original problem

9 / 84

Page 11: 18 Basic Graph Algorithms

No solution for a odd number of Bridges

What we wantThe (Eulerian) path should cross over each of the seven bridges exactlyonce

We cannot do this for the original problem

9 / 84

Page 12: 18 Basic Graph Algorithms

Necessary Condition

Euler discovered thatA necessary condition for the walk of the desired form is that the graph beconnected and have exactly zero or two nodes of odd degree.

Add an extra bridge

10 / 84

Page 13: 18 Basic Graph Algorithms

Necessary Condition

Euler discovered thatA necessary condition for the walk of the desired form is that the graph beconnected and have exactly zero or two nodes of odd degree.

Add an extra bridge

Add Extra Bridge

10 / 84

Page 14: 18 Basic Graph Algorithms

Studying Graphs

All the previous examples are telling usData Structures are required to design structures to hold the informationcoming from graphs!!!

Good RepresentationsThey will allow to handle the data structures with ease!!!

11 / 84

Page 15: 18 Basic Graph Algorithms

Studying Graphs

All the previous examples are telling usData Structures are required to design structures to hold the informationcoming from graphs!!!

Good RepresentationsThey will allow to handle the data structures with ease!!!

11 / 84

Page 16: 18 Basic Graph Algorithms

Outline

1 IntroductionGraphs EverywhereHistoryBasic TheoryRepresenting Graphs in a Computer

2 Traversing the GraphBreadth-first searchDepth-First Search

3 ApplicationsFinding a path between nodesConnected ComponentsSpanning TreesTopological Sorting

12 / 84

Page 17: 18 Basic Graph Algorithms

Basic TheoryDefinitionA Graph is composed of the following parts: Nodes and Edges

NodesThey can represent multiple things:

PeopleCitiesStates of Beingetc

EdgesThey can represent multiple things too:

Distance between citiesFriendshipsMatching StringsEtc

13 / 84

Page 18: 18 Basic Graph Algorithms

Basic TheoryDefinitionA Graph is composed of the following parts: Nodes and Edges

NodesThey can represent multiple things:

PeopleCitiesStates of Beingetc

EdgesThey can represent multiple things too:

Distance between citiesFriendshipsMatching StringsEtc

13 / 84

Page 19: 18 Basic Graph Algorithms

Basic TheoryDefinitionA Graph is composed of the following parts: Nodes and Edges

NodesThey can represent multiple things:

PeopleCitiesStates of Beingetc

EdgesThey can represent multiple things too:

Distance between citiesFriendshipsMatching StringsEtc

13 / 84

Page 20: 18 Basic Graph Algorithms

Basic TheoryDefinitionA Graph is composed of the following parts: Nodes and Edges

NodesThey can represent multiple things:

PeopleCitiesStates of Beingetc

EdgesThey can represent multiple things too:

Distance between citiesFriendshipsMatching StringsEtc

13 / 84

Page 21: 18 Basic Graph Algorithms

Basic TheoryDefinitionA Graph is composed of the following parts: Nodes and Edges

NodesThey can represent multiple things:

PeopleCitiesStates of Beingetc

EdgesThey can represent multiple things too:

Distance between citiesFriendshipsMatching StringsEtc

13 / 84

Page 22: 18 Basic Graph Algorithms

Basic TheoryDefinitionA Graph is composed of the following parts: Nodes and Edges

NodesThey can represent multiple things:

PeopleCitiesStates of Beingetc

EdgesThey can represent multiple things too:

Distance between citiesFriendshipsMatching StringsEtc

13 / 84

Page 23: 18 Basic Graph Algorithms

Basic TheoryDefinitionA Graph is composed of the following parts: Nodes and Edges

NodesThey can represent multiple things:

PeopleCitiesStates of Beingetc

EdgesThey can represent multiple things too:

Distance between citiesFriendshipsMatching StringsEtc

13 / 84

Page 24: 18 Basic Graph Algorithms

Basic TheoryDefinitionA Graph is composed of the following parts: Nodes and Edges

NodesThey can represent multiple things:

PeopleCitiesStates of Beingetc

EdgesThey can represent multiple things too:

Distance between citiesFriendshipsMatching StringsEtc

13 / 84

Page 25: 18 Basic Graph Algorithms

Basic TheoryDefinitionA Graph is composed of the following parts: Nodes and Edges

NodesThey can represent multiple things:

PeopleCitiesStates of Beingetc

EdgesThey can represent multiple things too:

Distance between citiesFriendshipsMatching StringsEtc

13 / 84

Page 26: 18 Basic Graph Algorithms

Basic TheoryDefinitionA Graph is composed of the following parts: Nodes and Edges

NodesThey can represent multiple things:

PeopleCitiesStates of Beingetc

EdgesThey can represent multiple things too:

Distance between citiesFriendshipsMatching StringsEtc

13 / 84

Page 27: 18 Basic Graph Algorithms

Basic Theory

DefinitionA graph G = (V ,E) is composed of a set of vertices (or nodes) V and aset of edges E , each assumed finite i.e. |V | = n and |E | = m.

Example

14 / 84

Page 28: 18 Basic Graph Algorithms

Basic Theory

DefinitionA graph G = (V ,E) is composed of a set of vertices (or nodes) V and aset of edges E , each assumed finite i.e. |V | = n and |E | = m.

Example

14 / 84

Page 29: 18 Basic Graph Algorithms

Properties

IncidentAn edge ek = (vi , vj) is incident with the vertices vi and vj .

A simple graph has no self-loops or multiple edges like below

15 / 84

Page 30: 18 Basic Graph Algorithms

Properties

IncidentAn edge ek = (vi , vj) is incident with the vertices vi and vj .

A simple graph has no self-loops or multiple edges like below

15 / 84

Page 31: 18 Basic Graph Algorithms

Some properties

DegreeThe degree d(v) of a vertex V is its number of incident edges

A self loopA self-loop counts for 2 in the degree function.

PropositionThe sum of the degrees of a graph G = (V ,E) equals 2|E | = 2m (trivial).

16 / 84

Page 32: 18 Basic Graph Algorithms

Some properties

DegreeThe degree d(v) of a vertex V is its number of incident edges

A self loopA self-loop counts for 2 in the degree function.

PropositionThe sum of the degrees of a graph G = (V ,E) equals 2|E | = 2m (trivial).

16 / 84

Page 33: 18 Basic Graph Algorithms

Some properties

DegreeThe degree d(v) of a vertex V is its number of incident edges

A self loopA self-loop counts for 2 in the degree function.

PropositionThe sum of the degrees of a graph G = (V ,E) equals 2|E | = 2m (trivial).

16 / 84

Page 34: 18 Basic Graph Algorithms

Some properties

CompleteA complete graph Kn is a simple graph with all n(n−1)/2 possible edges,like the graph below for n = 2, 3, 4, 5.

Example

17 / 84

Page 35: 18 Basic Graph Algorithms

Some properties

CompleteA complete graph Kn is a simple graph with all n(n−1)/2 possible edges,like the graph below for n = 2, 3, 4, 5.

Example

17 / 84

Page 36: 18 Basic Graph Algorithms

Outline

1 IntroductionGraphs EverywhereHistoryBasic TheoryRepresenting Graphs in a Computer

2 Traversing the GraphBreadth-first searchDepth-First Search

3 ApplicationsFinding a path between nodesConnected ComponentsSpanning TreesTopological Sorting

18 / 84

Page 37: 18 Basic Graph Algorithms

Clearly

We need to representNodesVertices

We need to representDirected edgesUndirected edges

19 / 84

Page 38: 18 Basic Graph Algorithms

Clearly

We need to representNodesVertices

We need to representDirected edgesUndirected edges

19 / 84

Page 39: 18 Basic Graph Algorithms

We need NICE representations of this definition

First OneAdjacency Representation

Second OneMatrix Representation

20 / 84

Page 40: 18 Basic Graph Algorithms

We need NICE representations of this definition

First OneAdjacency Representation

Second OneMatrix Representation

20 / 84

Page 41: 18 Basic Graph Algorithms

Adjacency-list representation

Basic DefinitionIt is an array of size |V| with

A list for each bucket representing a node telling us which nodes areconnected to it by one edge

21 / 84

Page 42: 18 Basic Graph Algorithms

Adjacency-list representation

Basic DefinitionIt is an array of size |V| with

A list for each bucket representing a node telling us which nodes areconnected to it by one edge

1

2

3

4

5

6

4

4 6

4

5

1

2

2 3 5

21 / 84

Page 43: 18 Basic Graph Algorithms

Properties

Space for storageFor undirected or directed graphs O (V + E)

Search: Successful or UnsuccessfulO(1 + degree(v))

In additionAdjacency lists can readily be adapted to represent weighted graphs

Weight function w : E → RThe weight w(u, v) of the edge (u, v) ∈ E is simply stored withvertex v in u’s adjacency list

22 / 84

Page 44: 18 Basic Graph Algorithms

Properties

Space for storageFor undirected or directed graphs O (V + E)

Search: Successful or UnsuccessfulO(1 + degree(v))

In additionAdjacency lists can readily be adapted to represent weighted graphs

Weight function w : E → RThe weight w(u, v) of the edge (u, v) ∈ E is simply stored withvertex v in u’s adjacency list

22 / 84

Page 45: 18 Basic Graph Algorithms

Properties

Space for storageFor undirected or directed graphs O (V + E)

Search: Successful or UnsuccessfulO(1 + degree(v))

In additionAdjacency lists can readily be adapted to represent weighted graphs

Weight function w : E → RThe weight w(u, v) of the edge (u, v) ∈ E is simply stored withvertex v in u’s adjacency list

22 / 84

Page 46: 18 Basic Graph Algorithms

Properties

Space for storageFor undirected or directed graphs O (V + E)

Search: Successful or UnsuccessfulO(1 + degree(v))

In additionAdjacency lists can readily be adapted to represent weighted graphs

Weight function w : E → RThe weight w(u, v) of the edge (u, v) ∈ E is simply stored withvertex v in u’s adjacency list

22 / 84

Page 47: 18 Basic Graph Algorithms

Possible Disadvantage

When looking to see if an edge existThere is no quicker way to determine if a given edge (u,v)

23 / 84

Page 48: 18 Basic Graph Algorithms

Adjacency Matrix Representation

In a natural way the edges can be identified by the nodesFor example, the edge between 1 and 4 nodes gets named as (1,4)

ThenHow, we use this to represent the graph through a Matrix or and Array ofArrays??!!!

24 / 84

Page 49: 18 Basic Graph Algorithms

Adjacency Matrix Representation

In a natural way the edges can be identified by the nodesFor example, the edge between 1 and 4 nodes gets named as (1,4)

ThenHow, we use this to represent the graph through a Matrix or and Array ofArrays??!!!

24 / 84

Page 50: 18 Basic Graph Algorithms

What about the following?

How do we indicate that an edge exist given the following matrix1 2 3 4 5 6

1 − − − − − −2 − − − − − −3 − − − − − −4 − − − − − −5 − − − − − −6 − − − − − −

You say it!!Use a 0 for no-edgeUse a 1 for edge

25 / 84

Page 51: 18 Basic Graph Algorithms

What about the following?

How do we indicate that an edge exist given the following matrix1 2 3 4 5 6

1 − − − − − −2 − − − − − −3 − − − − − −4 − − − − − −5 − − − − − −6 − − − − − −

You say it!!Use a 0 for no-edgeUse a 1 for edge

25 / 84

Page 52: 18 Basic Graph Algorithms

We have then...

Definition0/1 N ×N matrix with N =Number of nodes or verticesA(i, j) = 1 iff (i, j) is an edge

26 / 84

Page 53: 18 Basic Graph Algorithms

We have then...For the previous example

1 2 3 4 5 61 0 0 0 1 0 02 0 0 0 1 0 03 0 0 0 1 0 04 1 1 1 0 1 05 0 0 0 1 0 16 0 0 0 0 1 0

27 / 84

Page 54: 18 Basic Graph Algorithms

Properties of the Matrix for Undirected Graphs

Property OneDiagonal entries are zero.

Property TwoAdjacency matrix of an undirected graph is symmetric:

A (i, j) = A (j, i) for all i and j

28 / 84

Page 55: 18 Basic Graph Algorithms

Properties of the Matrix for Undirected Graphs

Property OneDiagonal entries are zero.

Property TwoAdjacency matrix of an undirected graph is symmetric:

A (i, j) = A (j, i) for all i and j

28 / 84

Page 56: 18 Basic Graph Algorithms

Complexity

Memory

Θ (V 2) (1)

Looking for an edgeO(1)

29 / 84

Page 57: 18 Basic Graph Algorithms

Complexity

Memory

Θ (V 2) (1)

Looking for an edgeO(1)

29 / 84

Page 58: 18 Basic Graph Algorithms

Traversing the Graph

Why do we need to traverse the graph?Do you have any examples?

YesSearch for paths satisfying various constraints

I Shortest Path

Visit some sets of verticesI Tours

Search if two graphs are equivalentI Isomorphisms

30 / 84

Page 59: 18 Basic Graph Algorithms

Traversing the Graph

Why do we need to traverse the graph?Do you have any examples?

YesSearch for paths satisfying various constraints

I Shortest Path

Visit some sets of verticesI Tours

Search if two graphs are equivalentI Isomorphisms

30 / 84

Page 60: 18 Basic Graph Algorithms

Traversing the Graph

Why do we need to traverse the graph?Do you have any examples?

YesSearch for paths satisfying various constraints

I Shortest Path

Visit some sets of verticesI Tours

Search if two graphs are equivalentI Isomorphisms

30 / 84

Page 61: 18 Basic Graph Algorithms

Traversing the Graph

Why do we need to traverse the graph?Do you have any examples?

YesSearch for paths satisfying various constraints

I Shortest Path

Visit some sets of verticesI Tours

Search if two graphs are equivalentI Isomorphisms

30 / 84

Page 62: 18 Basic Graph Algorithms

Outline

1 IntroductionGraphs EverywhereHistoryBasic TheoryRepresenting Graphs in a Computer

2 Traversing the GraphBreadth-first searchDepth-First Search

3 ApplicationsFinding a path between nodesConnected ComponentsSpanning TreesTopological Sorting

31 / 84

Page 63: 18 Basic Graph Algorithms

Breadth-first search

DefinitionGiven a graph G = (V ,E) and a source vertex s, breadth-first searchsystematically explores the edges of Gto “discover” every vertex that isreachable from the vertex s

Something NotableA vertex is discovered the first time it is encountered during the search

32 / 84

Page 64: 18 Basic Graph Algorithms

Breadth-first search

DefinitionGiven a graph G = (V ,E) and a source vertex s, breadth-first searchsystematically explores the edges of Gto “discover” every vertex that isreachable from the vertex s

Something NotableA vertex is discovered the first time it is encountered during the search

32 / 84

Page 65: 18 Basic Graph Algorithms

Breadth-First Search Algorithm

AlgorithmBFS(G, s)1. for each vertex u ∈ G.V − {s}2. u.color = WHITE3. u.d =∞4. u.π = NIL5. s.color =GRAY6. s.d = 07. s.π = NIL8. Q = ∅9. Enqueue(Q, s)

10. while Q 6= ∅11. u =Dequeue(Q)12. for each v ∈ G.Adj [u]13. if v.color ==WHITE14. v.color =GRAY15. v.d = u.d + 116. v.π = u17. Enqueue(Q, v)18. u.color = BLACK

33 / 84

Page 66: 18 Basic Graph Algorithms

Breadth-First Search Algorithm

AlgorithmBFS(G, s)1. for each vertex u ∈ G.V − {s}2. u.color = WHITE3. u.d =∞4. u.π = NIL5. s.color =GRAY6. s.d = 07. s.π = NIL8. Q = ∅9. Enqueue(Q, s)

10. while Q 6= ∅11. u =Dequeue(Q)12. for each v ∈ G.Adj [u]13. if v.color ==WHITE14. v.color =GRAY15. v.d = u.d + 116. v.π = u17. Enqueue(Q, v)18. u.color = BLACK

33 / 84

Page 67: 18 Basic Graph Algorithms

Breadth-First Search Algorithm

AlgorithmBFS(G, s)1. for each vertex u ∈ G.V − {s}2. u.color = WHITE3. u.d =∞4. u.π = NIL5. s.color =GRAY6. s.d = 07. s.π = NIL8. Q = ∅9. Enqueue(Q, s)

10. while Q 6= ∅11. u =Dequeue(Q)12. for each v ∈ G.Adj [u]13. if v.color ==WHITE14. v.color =GRAY15. v.d = u.d + 116. v.π = u17. Enqueue(Q, v)18. u.color = BLACK

33 / 84

Page 68: 18 Basic Graph Algorithms

Breadth-First Search Algorithm

AlgorithmBFS(G, s)1. for each vertex u ∈ G.V − {s}2. u.color = WHITE3. u.d =∞4. u.π = NIL5. s.color =GRAY6. s.d = 07. s.π = NIL8. Q = ∅9. Enqueue(Q, s)

10. while Q 6= ∅11. u =Dequeue(Q)12. for each v ∈ G.Adj [u]13. if v.color ==WHITE14. v.color =GRAY15. v.d = u.d + 116. v.π = u17. Enqueue(Q, v)18. u.color = BLACK

33 / 84

Page 69: 18 Basic Graph Algorithms

Breadth-First Search Algorithm

AlgorithmBFS(G, s)1. for each vertex u ∈ G.V − {s}2. u.color = WHITE3. u.d =∞4. u.π = NIL5. s.color =GRAY6. s.d = 07. s.π = NIL8. Q = ∅9. Enqueue(Q, s)

10. while Q 6= ∅11. u =Dequeue(Q)12. for each v ∈ G.Adj [u]13. if v.color ==WHITE14. v.color =GRAY15. v.d = u.d + 116. v.π = u17. Enqueue(Q, v)18. u.color = BLACK

33 / 84

Page 70: 18 Basic Graph Algorithms

Breadth-First Search Algorithm

AlgorithmBFS(G, s)1. for each vertex u ∈ G.V − {s}2. u.color = WHITE3. u.d =∞4. u.π = NIL5. s.color =GRAY6. s.d = 07. s.π = NIL8. Q = ∅9. Enqueue(Q, s)

10. while Q 6= ∅11. u =Dequeue(Q)12. for each v ∈ G.Adj [u]13. if v.color ==WHITE14. v.color =GRAY15. v.d = u.d + 116. v.π = u17. Enqueue(Q, v)18. u.color = BLACK

33 / 84

Page 71: 18 Basic Graph Algorithms

Breadth-First Search Algorithm

AlgorithmBFS(G, s)1. for each vertex u ∈ G.V − {s}2. u.color = WHITE3. u.d =∞4. u.π = NIL5. s.color =GRAY6. s.d = 07. s.π = NIL8. Q = ∅9. Enqueue(Q, s)

10. while Q 6= ∅11. u =Dequeue(Q)12. for each v ∈ G.Adj [u]13. if v.color ==WHITE14. v.color =GRAY15. v.d = u.d + 116. v.π = u17. Enqueue(Q, v)18. u.color = BLACK

33 / 84

Page 72: 18 Basic Graph Algorithms

BFS allows to change the order of recursion

Remember

1

2

3 4

5

6

34 / 84

Page 73: 18 Basic Graph Algorithms

Loop Invariance

The While loopThis while loop maintains the following invariant :

At the test in line 10, the queue Q consists of the set of gray vertices

First iterationQ = sand s.color = GRAY

MaintenanceThe inner loop only pushes gray nodes into the queue.

35 / 84

Page 74: 18 Basic Graph Algorithms

Loop Invariance

The While loopThis while loop maintains the following invariant :

At the test in line 10, the queue Q consists of the set of gray vertices

First iterationQ = sand s.color = GRAY

MaintenanceThe inner loop only pushes gray nodes into the queue.

35 / 84

Page 75: 18 Basic Graph Algorithms

Loop Invariance

The While loopThis while loop maintains the following invariant :

At the test in line 10, the queue Q consists of the set of gray vertices

First iterationQ = sand s.color = GRAY

MaintenanceThe inner loop only pushes gray nodes into the queue.

35 / 84

Page 76: 18 Basic Graph Algorithms

Loop Invariance

TerminationWhen every node that can be visited is painted black

36 / 84

Page 77: 18 Basic Graph Algorithms

Example

What do you see?

0

0

37 / 84

Page 78: 18 Basic Graph Algorithms

Example

What do you see?

0

1 1

1

1

38 / 84

Page 79: 18 Basic Graph Algorithms

Example

What do you see?

0

1 2

1

12

39 / 84

Page 80: 18 Basic Graph Algorithms

Example

What do you see?

0

2 2 2

1

12

2

2

40 / 84

Page 81: 18 Basic Graph Algorithms

Example

What do you see?

0

2 2

1

12

2

2

41 / 84

Page 82: 18 Basic Graph Algorithms

Example

What do you see?

0

2 3

1

12

2

2

3

42 / 84

Page 83: 18 Basic Graph Algorithms

Example

What do you see?

0

3 3

1

12

2

2

3

3

43 / 84

Page 84: 18 Basic Graph Algorithms

Example

What do you see?

0

3

1

12

2

2

3

3

44 / 84

Page 85: 18 Basic Graph Algorithms

Example

What do you see?

01

12

2

2

3

3

45 / 84

Page 86: 18 Basic Graph Algorithms

Complexity

What about the outer loop?O(V ) Enqueue / Dequeue operations – Each adjacency list is processedonly once.

What about the inner loop?The sum of the lengths of f all the adjacency lists is Θ(E) so the scanningtakes O(E)

46 / 84

Page 87: 18 Basic Graph Algorithms

Complexity

What about the outer loop?O(V ) Enqueue / Dequeue operations – Each adjacency list is processedonly once.

What about the inner loop?The sum of the lengths of f all the adjacency lists is Θ(E) so the scanningtakes O(E)

46 / 84

Page 88: 18 Basic Graph Algorithms

Complexity

Overhead of CreationO (V )

ThenTotal complexity O (V + E)

47 / 84

Page 89: 18 Basic Graph Algorithms

Complexity

Overhead of CreationO (V )

ThenTotal complexity O (V + E)

47 / 84

Page 90: 18 Basic Graph Algorithms

Properties: Predecessor Graph

Something NotableBreadth-first search constructs a breadth-first tree, initially containing onlyits root, which is the source vertex s

ThusWe say that u is the predecessor or parent of v in the breadth-first tree.

48 / 84

Page 91: 18 Basic Graph Algorithms

Properties: Predecessor Graph

Something NotableBreadth-first search constructs a breadth-first tree, initially containing onlyits root, which is the source vertex s

ThusWe say that u is the predecessor or parent of v in the breadth-first tree.

48 / 84

Page 92: 18 Basic Graph Algorithms

For example

From the previous example

Predecessor Graph

49 / 84

Page 93: 18 Basic Graph Algorithms

For example

From the previous example

Predecessor Graphs

r w

v t x

yu

49 / 84

Page 94: 18 Basic Graph Algorithms

This allow to use the Algorithm for finding The ShortestPath

ClearlyThis is the unweighted version or all weights are equal!!!

We have the following functionδ (s, v)= shortest path from s to v

We claim thatUpon termination of BFS, every vertex v ∈ V reachable from s has

v.d = δ(s, v)

50 / 84

Page 95: 18 Basic Graph Algorithms

This allow to use the Algorithm for finding The ShortestPath

ClearlyThis is the unweighted version or all weights are equal!!!

We have the following functionδ (s, v)= shortest path from s to v

We claim thatUpon termination of BFS, every vertex v ∈ V reachable from s has

v.d = δ(s, v)

50 / 84

Page 96: 18 Basic Graph Algorithms

This allow to use the Algorithm for finding The ShortestPath

ClearlyThis is the unweighted version or all weights are equal!!!

We have the following functionδ (s, v)= shortest path from s to v

We claim thatUpon termination of BFS, every vertex v ∈ V reachable from s has

v.d = δ(s, v)

50 / 84

Page 97: 18 Basic Graph Algorithms

Intuitive Idea of Claim

Correctness of breadth-first searchLet G = (V ,E) be a directed or undirected graph.Suppose that BFS is run on G from a given source vertex s ∈ V .

ThenThen, during its execution, BFS discovers every vertex v ∈ V that isreachable from the source.

51 / 84

Page 98: 18 Basic Graph Algorithms

Intuitive Idea of Claim

Correctness of breadth-first searchLet G = (V ,E) be a directed or undirected graph.Suppose that BFS is run on G from a given source vertex s ∈ V .

ThenThen, during its execution, BFS discovers every vertex v ∈ V that isreachable from the source.

51 / 84

Page 99: 18 Basic Graph Algorithms

Intuitive Idea of Claim

Correctness of breadth-first searchLet G = (V ,E) be a directed or undirected graph.Suppose that BFS is run on G from a given source vertex s ∈ V .

ThenThen, during its execution, BFS discovers every vertex v ∈ V that isreachable from the source.

51 / 84

Page 100: 18 Basic Graph Algorithms

Intuitive Idea of Claim

Distance IdeaFirst, once a node u is reached from v, we use the previous shortestdistance from s to update the node distance.

52 / 84

Page 101: 18 Basic Graph Algorithms

Intuitive Idea of Claim

You can use the idea of strong inductionGiven a branch of the breadth-first search u s, u1, u2, ..., un

un .π = un−1.π + 1 = un−1.π + 2 = n + s.π = n (2)

Thus, we have thatFor any vertex v 6= s that is reachable from s, one of the shortestpath from s to v is

I A shortest path from s to v.π followed by thew edge (v.π, v).

53 / 84

Page 102: 18 Basic Graph Algorithms

Intuitive Idea of Claim

You can use the idea of strong inductionGiven a branch of the breadth-first search u s, u1, u2, ..., un

un .π = un−1.π + 1 = un−1.π + 2 = n + s.π = n (2)

Thus, we have thatFor any vertex v 6= s that is reachable from s, one of the shortestpath from s to v is

I A shortest path from s to v.π followed by thew edge (v.π, v).

53 / 84

Page 103: 18 Basic Graph Algorithms

Outline

1 IntroductionGraphs EverywhereHistoryBasic TheoryRepresenting Graphs in a Computer

2 Traversing the GraphBreadth-first searchDepth-First Search

3 ApplicationsFinding a path between nodesConnected ComponentsSpanning TreesTopological Sorting

54 / 84

Page 104: 18 Basic Graph Algorithms

Depth-first search

Given GPick an unvisited vertex v, remember the rest.

I Recurse on vertices adjacent to v

55 / 84

Page 105: 18 Basic Graph Algorithms

The Pseudo-code

Code for DFSDFS(G)1. for each vertex u ∈ G.V2. u.color = WHITE3. u.π = NIL4. time = 05. for each vertex u ∈ G.V6. if u.color = WHITE7.

DFS-VISIT(G, u)

DFS-VISIT(G, u)1. time = time + 12. u.d = time3. u.color = GRAY4. for each vertex v ∈ G.Adj [u]5. if v.color == WHITE6. v.π = u7. DFS-VISIT(G, v)8. u.color = BLACK9. time = time + 1

10. u.f = time

56 / 84

Page 106: 18 Basic Graph Algorithms

The Pseudo-code

Code for DFSDFS(G)1. for each vertex u ∈ G.V2. u.color = WHITE3. u.π = NIL4. time = 05. for each vertex u ∈ G.V6. if u.color = WHITE7.

DFS-VISIT(G, u)

DFS-VISIT(G, u)1. time = time + 12. u.d = time3. u.color = GRAY4. for each vertex v ∈ G.Adj [u]5. if v.color == WHITE6. v.π = u7. DFS-VISIT(G, v)8. u.color = BLACK9. time = time + 1

10. u.f = time

56 / 84

Page 107: 18 Basic Graph Algorithms

The Pseudo-code

Code for DFSDFS(G)1. for each vertex u ∈ G.V2. u.color = WHITE3. u.π = NIL4. time = 05. for each vertex u ∈ G.V6. if u.color = WHITE7.

DFS-VISIT(G, u)

DFS-VISIT(G, u)1. time = time + 12. u.d = time3. u.color = GRAY4. for each vertex v ∈ G.Adj [u]5. if v.color == WHITE6. v.π = u7. DFS-VISIT(G, v)8. u.color = BLACK9. time = time + 1

10. u.f = time

56 / 84

Page 108: 18 Basic Graph Algorithms

The Pseudo-code

Code for DFSDFS(G)1. for each vertex u ∈ G.V2. u.color = WHITE3. u.π = NIL4. time = 05. for each vertex u ∈ G.V6. if u.color = WHITE7.

DFS-VISIT(G, u)

DFS-VISIT(G, u)1. time = time + 12. u.d = time3. u.color = GRAY4. for each vertex v ∈ G.Adj [u]5. if v.color == WHITE6. v.π = u7. DFS-VISIT(G, v)8. u.color = BLACK9. time = time + 1

10. u.f = time

56 / 84

Page 109: 18 Basic Graph Algorithms

The Pseudo-code

Code for DFSDFS(G)1. for each vertex u ∈ G.V2. u.color = WHITE3. u.π = NIL4. time = 05. for each vertex u ∈ G.V6. if u.color = WHITE7.

DFS-VISIT(G, u)

DFS-VISIT(G, u)1. time = time + 12. u.d = time3. u.color = GRAY4. for each vertex v ∈ G.Adj [u]5. if v.color == WHITE6. v.π = u7. DFS-VISIT(G, v)8. u.color = BLACK9. time = time + 1

10. u.f = time

56 / 84

Page 110: 18 Basic Graph Algorithms

The Pseudo-code

Code for DFSDFS(G)1. for each vertex u ∈ G.V2. u.color = WHITE3. u.π = NIL4. time = 05. for each vertex u ∈ G.V6. if u.color = WHITE7.

DFS-VISIT(G, u)

DFS-VISIT(G, u)1. time = time + 12. u.d = time3. u.color = GRAY4. for each vertex v ∈ G.Adj [u]5. if v.color == WHITE6. v.π = u7. DFS-VISIT(G, v)8. u.color = BLACK9. time = time + 1

10. u.f = time

56 / 84

Page 111: 18 Basic Graph Algorithms

The Pseudo-code

Code for DFSDFS(G)1. for each vertex u ∈ G.V2. u.color = WHITE3. u.π = NIL4. time = 05. for each vertex u ∈ G.V6. if u.color = WHITE7.

DFS-VISIT(G, u)

DFS-VISIT(G, u)1. time = time + 12. u.d = time3. u.color = GRAY4. for each vertex v ∈ G.Adj [u]5. if v.color == WHITE6. v.π = u7. DFS-VISIT(G, v)8. u.color = BLACK9. time = time + 1

10. u.f = time

56 / 84

Page 112: 18 Basic Graph Algorithms

Example

What do we do?

57 / 84

Page 113: 18 Basic Graph Algorithms

Example

What do we do?

58 / 84

Page 114: 18 Basic Graph Algorithms

Example

What do we do?

59 / 84

Page 115: 18 Basic Graph Algorithms

Example

What do we do?

60 / 84

Page 116: 18 Basic Graph Algorithms

Example

What do we do?

61 / 84

Page 117: 18 Basic Graph Algorithms

Example

What do we do?

62 / 84

Page 118: 18 Basic Graph Algorithms

Example

What do we do?

63 / 84

Page 119: 18 Basic Graph Algorithms

Example

What do we do?

64 / 84

Page 120: 18 Basic Graph Algorithms

Complexity

Analysis1 The loops on lines 1–3 and lines 5–7 of DFS take Θ (V ).2 The procedure DFS-VISIT is called exactly once for each vertex

v ∈ V .3 During an execution of DFS-VISIT(G, v) the loop on lines 4–7

executes |Adj (v)| times.4 But ∑v∈V |Adj (v)| = Θ (E) we have that the cost of executing g

lines 4–7 of DFS-VISIT is Θ (E) .

ThenDFS complexity is Θ (V + E)

65 / 84

Page 121: 18 Basic Graph Algorithms

Complexity

Analysis1 The loops on lines 1–3 and lines 5–7 of DFS take Θ (V ).2 The procedure DFS-VISIT is called exactly once for each vertex

v ∈ V .3 During an execution of DFS-VISIT(G, v) the loop on lines 4–7

executes |Adj (v)| times.4 But ∑v∈V |Adj (v)| = Θ (E) we have that the cost of executing g

lines 4–7 of DFS-VISIT is Θ (E) .

ThenDFS complexity is Θ (V + E)

65 / 84

Page 122: 18 Basic Graph Algorithms

Complexity

Analysis1 The loops on lines 1–3 and lines 5–7 of DFS take Θ (V ).2 The procedure DFS-VISIT is called exactly once for each vertex

v ∈ V .3 During an execution of DFS-VISIT(G, v) the loop on lines 4–7

executes |Adj (v)| times.4 But ∑v∈V |Adj (v)| = Θ (E) we have that the cost of executing g

lines 4–7 of DFS-VISIT is Θ (E) .

ThenDFS complexity is Θ (V + E)

65 / 84

Page 123: 18 Basic Graph Algorithms

Complexity

Analysis1 The loops on lines 1–3 and lines 5–7 of DFS take Θ (V ).2 The procedure DFS-VISIT is called exactly once for each vertex

v ∈ V .3 During an execution of DFS-VISIT(G, v) the loop on lines 4–7

executes |Adj (v)| times.4 But ∑v∈V |Adj (v)| = Θ (E) we have that the cost of executing g

lines 4–7 of DFS-VISIT is Θ (E) .

ThenDFS complexity is Θ (V + E)

65 / 84

Page 124: 18 Basic Graph Algorithms

Complexity

Analysis1 The loops on lines 1–3 and lines 5–7 of DFS take Θ (V ).2 The procedure DFS-VISIT is called exactly once for each vertex

v ∈ V .3 During an execution of DFS-VISIT(G, v) the loop on lines 4–7

executes |Adj (v)| times.4 But ∑v∈V |Adj (v)| = Θ (E) we have that the cost of executing g

lines 4–7 of DFS-VISIT is Θ (E) .

ThenDFS complexity is Θ (V + E)

65 / 84

Page 125: 18 Basic Graph Algorithms

Applications

We have severalTopological SortStrongly Connected ComponentsComputer Vision AlgorithmsArtificial Intelligence AlgorithmsImportance in Social NetworkRank Algorithms for GoogleEtc.

66 / 84

Page 126: 18 Basic Graph Algorithms

Applications

We have severalTopological SortStrongly Connected ComponentsComputer Vision AlgorithmsArtificial Intelligence AlgorithmsImportance in Social NetworkRank Algorithms for GoogleEtc.

66 / 84

Page 127: 18 Basic Graph Algorithms

Applications

We have severalTopological SortStrongly Connected ComponentsComputer Vision AlgorithmsArtificial Intelligence AlgorithmsImportance in Social NetworkRank Algorithms for GoogleEtc.

66 / 84

Page 128: 18 Basic Graph Algorithms

Applications

We have severalTopological SortStrongly Connected ComponentsComputer Vision AlgorithmsArtificial Intelligence AlgorithmsImportance in Social NetworkRank Algorithms for GoogleEtc.

66 / 84

Page 129: 18 Basic Graph Algorithms

Applications

We have severalTopological SortStrongly Connected ComponentsComputer Vision AlgorithmsArtificial Intelligence AlgorithmsImportance in Social NetworkRank Algorithms for GoogleEtc.

66 / 84

Page 130: 18 Basic Graph Algorithms

Applications

We have severalTopological SortStrongly Connected ComponentsComputer Vision AlgorithmsArtificial Intelligence AlgorithmsImportance in Social NetworkRank Algorithms for GoogleEtc.

66 / 84

Page 131: 18 Basic Graph Algorithms

Applications

We have severalTopological SortStrongly Connected ComponentsComputer Vision AlgorithmsArtificial Intelligence AlgorithmsImportance in Social NetworkRank Algorithms for GoogleEtc.

66 / 84

Page 132: 18 Basic Graph Algorithms

Outline

1 IntroductionGraphs EverywhereHistoryBasic TheoryRepresenting Graphs in a Computer

2 Traversing the GraphBreadth-first searchDepth-First Search

3 ApplicationsFinding a path between nodesConnected ComponentsSpanning TreesTopological Sorting

67 / 84

Page 133: 18 Basic Graph Algorithms

Finding a path between nodes

We do the followingStart a breadth-first search at vertex v.Terminate when vertex u is visited or when Q becomes empty(whichever occurs first).

Time ComplexityO(V 2)when adjacency matrix used.

O(V + E) when adjacency lists used.

68 / 84

Page 134: 18 Basic Graph Algorithms

Finding a path between nodes

We do the followingStart a breadth-first search at vertex v.Terminate when vertex u is visited or when Q becomes empty(whichever occurs first).

Time ComplexityO(V 2)when adjacency matrix used.

O(V + E) when adjacency lists used.

68 / 84

Page 135: 18 Basic Graph Algorithms

Finding a path between nodes

We do the followingStart a breadth-first search at vertex v.Terminate when vertex u is visited or when Q becomes empty(whichever occurs first).

Time ComplexityO(V 2)when adjacency matrix used.

O(V + E) when adjacency lists used.

68 / 84

Page 136: 18 Basic Graph Algorithms

Finding a path between nodes

We do the followingStart a breadth-first search at vertex v.Terminate when vertex u is visited or when Q becomes empty(whichever occurs first).

Time ComplexityO(V 2)when adjacency matrix used.

O(V + E) when adjacency lists used.

68 / 84

Page 137: 18 Basic Graph Algorithms

This allow to use the Algorithm for finding The ShortestPath

ClearlyThis is the unweighted version or all weights are equal!!!

We have the following functionδ (s, v)= shortest path from s to v

ActuallyUpon termination of BFS, every vertex v ∈ V reachable from s hasdistance(v) = δ(s, v)

69 / 84

Page 138: 18 Basic Graph Algorithms

This allow to use the Algorithm for finding The ShortestPath

ClearlyThis is the unweighted version or all weights are equal!!!

We have the following functionδ (s, v)= shortest path from s to v

ActuallyUpon termination of BFS, every vertex v ∈ V reachable from s hasdistance(v) = δ(s, v)

69 / 84

Page 139: 18 Basic Graph Algorithms

This allow to use the Algorithm for finding The ShortestPath

ClearlyThis is the unweighted version or all weights are equal!!!

We have the following functionδ (s, v)= shortest path from s to v

ActuallyUpon termination of BFS, every vertex v ∈ V reachable from s hasdistance(v) = δ(s, v)

69 / 84

Page 140: 18 Basic Graph Algorithms

Outline

1 IntroductionGraphs EverywhereHistoryBasic TheoryRepresenting Graphs in a Computer

2 Traversing the GraphBreadth-first searchDepth-First Search

3 ApplicationsFinding a path between nodesConnected ComponentsSpanning TreesTopological Sorting

70 / 84

Page 141: 18 Basic Graph Algorithms

Connected Components

DefinitionA connected component (or just component) of an undirected graph is asubgraph in which any two vertices are connected to each other by paths.

Example

71 / 84

Page 142: 18 Basic Graph Algorithms

Connected ComponentsDefinitionA connected component (or just component) of an undirected graph is asubgraph in which any two vertices are connected to each other by paths.

Example

71 / 84

Page 143: 18 Basic Graph Algorithms

Procedure

FirstStart a breadth-first search at any as yet unvisited vertex of the graph.

ThusNewly visited vertices (plus edges between them) define a component.

RepeatRepeat until all vertices are visited.

72 / 84

Page 144: 18 Basic Graph Algorithms

Procedure

FirstStart a breadth-first search at any as yet unvisited vertex of the graph.

ThusNewly visited vertices (plus edges between them) define a component.

RepeatRepeat until all vertices are visited.

72 / 84

Page 145: 18 Basic Graph Algorithms

Procedure

FirstStart a breadth-first search at any as yet unvisited vertex of the graph.

ThusNewly visited vertices (plus edges between them) define a component.

RepeatRepeat until all vertices are visited.

72 / 84

Page 146: 18 Basic Graph Algorithms

Time

O (V 2)When adjacency matrix used

O(V + E)When adjacency lists used (E is number of edges)

73 / 84

Page 147: 18 Basic Graph Algorithms

Time

O (V 2)When adjacency matrix used

O(V + E)When adjacency lists used (E is number of edges)

73 / 84

Page 148: 18 Basic Graph Algorithms

Outline

1 IntroductionGraphs EverywhereHistoryBasic TheoryRepresenting Graphs in a Computer

2 Traversing the GraphBreadth-first searchDepth-First Search

3 ApplicationsFinding a path between nodesConnected ComponentsSpanning TreesTopological Sorting

74 / 84

Page 149: 18 Basic Graph Algorithms

Spanning Tree with edges with same weight of no weight

DefinitionA spanning tree of a graph G = (V ,E) is a acyclic graph where foru, v ∈ V , there is a path between them

Example

75 / 84

Page 150: 18 Basic Graph Algorithms

Spanning Tree with edges with same weight of no weight

DefinitionA spanning tree of a graph G = (V ,E) is a acyclic graph where foru, v ∈ V , there is a path between them

Example2

3

5

1

4

6

7

9

8

75 / 84

Page 151: 18 Basic Graph Algorithms

Procedure

FirstStart a breadth-first search at any vertex of the graph.

ThusIf graph is connected, the n − 1 edges used to get to unvisited verticesdefine a spanning tree (breadth-first spanning tree).

76 / 84

Page 152: 18 Basic Graph Algorithms

Procedure

FirstStart a breadth-first search at any vertex of the graph.

ThusIf graph is connected, the n − 1 edges used to get to unvisited verticesdefine a spanning tree (breadth-first spanning tree).

76 / 84

Page 153: 18 Basic Graph Algorithms

Time

O (V 2)When adjacency matrix used

O(V + E)When adjacency lists used (E is number of edges)

77 / 84

Page 154: 18 Basic Graph Algorithms

Time

O (V 2)When adjacency matrix used

O(V + E)When adjacency lists used (E is number of edges)

77 / 84

Page 155: 18 Basic Graph Algorithms

Outline

1 IntroductionGraphs EverywhereHistoryBasic TheoryRepresenting Graphs in a Computer

2 Traversing the GraphBreadth-first searchDepth-First Search

3 ApplicationsFinding a path between nodesConnected ComponentsSpanning TreesTopological Sorting

78 / 84

Page 156: 18 Basic Graph Algorithms

Topological Sorting

DefinitionsA topological sort (sometimes abbreviated topsort or toposort) ortopological ordering of a directed graph is a linear ordering of its verticessuch that for every directed edge (u, v) from vertex u to vertex y, u comesbefore v in the ordering.

From Industrial EngineeringThe canonical application of topological sorting (topological order) isin scheduling a sequence of jobs or tasks based on their dependencies.Topological sorting algorithms were first studied in the early 1960s inthe context of the PERT technique for scheduling in projectmanagement (Jarnagin 1960).

79 / 84

Page 157: 18 Basic Graph Algorithms

Topological Sorting

DefinitionsA topological sort (sometimes abbreviated topsort or toposort) ortopological ordering of a directed graph is a linear ordering of its verticessuch that for every directed edge (u, v) from vertex u to vertex y, u comesbefore v in the ordering.

From Industrial EngineeringThe canonical application of topological sorting (topological order) isin scheduling a sequence of jobs or tasks based on their dependencies.Topological sorting algorithms were first studied in the early 1960s inthe context of the PERT technique for scheduling in projectmanagement (Jarnagin 1960).

79 / 84

Page 158: 18 Basic Graph Algorithms

Then

We have thatThe jobs are represented by vertices, and there is an edge from x to y ifjob x must be completed before job y can be started.

ExampleWhen washing clothes, the washing machine must finish before we put theclothes to dry.

ThenA topological sort gives an order in which to perform the jobs.

80 / 84

Page 159: 18 Basic Graph Algorithms

Then

We have thatThe jobs are represented by vertices, and there is an edge from x to y ifjob x must be completed before job y can be started.

ExampleWhen washing clothes, the washing machine must finish before we put theclothes to dry.

ThenA topological sort gives an order in which to perform the jobs.

80 / 84

Page 160: 18 Basic Graph Algorithms

Then

We have thatThe jobs are represented by vertices, and there is an edge from x to y ifjob x must be completed before job y can be started.

ExampleWhen washing clothes, the washing machine must finish before we put theclothes to dry.

ThenA topological sort gives an order in which to perform the jobs.

80 / 84

Page 161: 18 Basic Graph Algorithms

Algorithm

Pseudo CodeTOPOLOGICAL-SORT

1 Call DFS(G) to compute finishing times v.f for each vertex v.2 As each vertex is finished, insert it onto the front of a linked list3 Return the linked list of vertices

81 / 84

Page 162: 18 Basic Graph Algorithms

Algorithm

Pseudo CodeTOPOLOGICAL-SORT

1 Call DFS(G) to compute finishing times v.f for each vertex v.2 As each vertex is finished, insert it onto the front of a linked list3 Return the linked list of vertices

81 / 84

Page 163: 18 Basic Graph Algorithms

Algorithm

Pseudo CodeTOPOLOGICAL-SORT

1 Call DFS(G) to compute finishing times v.f for each vertex v.2 As each vertex is finished, insert it onto the front of a linked list3 Return the linked list of vertices

81 / 84

Page 164: 18 Basic Graph Algorithms

Example

Dressing

undershortsocks

pants

beltshirt

tie

jacket

shoes

watch11/16

12/15

6/7

1/8

2/5

3/4

17/18

13/14

u.d/u.f

9/10

82 / 84

Page 165: 18 Basic Graph Algorithms

Thus

Using the u.fAs each vertex is finished, insert it onto the front of alinked list

83 / 84

Page 166: 18 Basic Graph Algorithms

Example

After Sorting

undershortsocks pants beltshirt tie jacketshoes watch

11/16 12/15 6/71/8 2/5 3/417/18 13/14 9/10

84 / 84