Top Banner
Data Structures Graphs Andres Mendez-Vazquez May 8, 2015 1 / 91
196
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Preparation Data Structures 11 graphs

Data StructuresGraphs

Andres Mendez-Vazquez

May 8, 2015

1 / 91

Page 2: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Outline1 Graphs

Graphs EverywhereHistoryBasic Theory

2 Graph RepresentationMatrix RepresentationPossible Code for This RepresentationAdjacency List Representation

3 Traversing the GraphBreadth-first searchDepth-First Search

4 ApplicationsFinding a path between nodesConnected ComponentsSpanning TreesTopological Sorting

2 / 91

Page 3: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Outline1 Graphs

Graphs EverywhereHistoryBasic Theory

2 Graph RepresentationMatrix RepresentationPossible Code for This RepresentationAdjacency List Representation

3 Traversing the GraphBreadth-first searchDepth-First Search

4 ApplicationsFinding a path between nodesConnected ComponentsSpanning TreesTopological Sorting

3 / 91

Page 4: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

We are full of Graphs

Maps

4 / 91

Page 5: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

We are full of Graphs

State Machines for Branch Prediction in CPU’s

5 / 91

Page 6: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

We are full of Graphs

Social Networks

6 / 91

Page 7: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Outline1 Graphs

Graphs EverywhereHistoryBasic Theory

2 Graph RepresentationMatrix RepresentationPossible Code for This RepresentationAdjacency List Representation

3 Traversing the GraphBreadth-first searchDepth-First Search

4 ApplicationsFinding a path between nodesConnected ComponentsSpanning TreesTopological Sorting

7 / 91

Page 8: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

History

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

The Actual City

8 / 91

Page 9: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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

The Actual City

8 / 91

Page 10: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

There is No-Solution

Using GraphsThe (Eulerian) path should cross over each of the seven bridges exactlyonce

Can you find Any possible solution?

9 / 91

Page 11: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

There is No-Solution

Using GraphsThe (Eulerian) path should cross over each of the seven bridges exactlyonce

Can you find Any possible solution?

9 / 91

Page 12: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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 / 91

Page 13: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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 / 91

Page 14: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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 / 91

Page 15: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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 / 91

Page 16: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Outline1 Graphs

Graphs EverywhereHistoryBasic Theory

2 Graph RepresentationMatrix RepresentationPossible Code for This RepresentationAdjacency List Representation

3 Traversing the GraphBreadth-first searchDepth-First Search

4 ApplicationsFinding a path between nodesConnected ComponentsSpanning TreesTopological Sorting

12 / 91

Page 17: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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 / 91

Page 18: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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 / 91

Page 19: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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 / 91

Page 20: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Basic Theory

DefinitionA graph G = (V ,E ) consists of a set of vertices (or nodes) V and a set ofedges E , where we assume that they are finite i.e. |V | = n and |E | = m.

Example

14 / 91

Page 21: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Basic Theory

DefinitionA graph G = (V ,E ) consists of a set of vertices (or nodes) V and a set ofedges E , where we assume that they are finite i.e. |V | = n and |E | = m.

Example

14 / 91

Page 22: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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 / 91

Page 23: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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 / 91

Page 24: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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 / 91

Page 25: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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 / 91

Page 26: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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 / 91

Page 27: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Empty

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

Example

17 / 91

Page 28: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Empty

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

Example

17 / 91

Page 29: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

We need NICE representations

First OneMatrix Representation

Second OneAdjacency Representation

18 / 91

Page 30: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

We need NICE representations

First OneMatrix Representation

Second OneAdjacency Representation

18 / 91

Page 31: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Outline1 Graphs

Graphs EverywhereHistoryBasic Theory

2 Graph RepresentationMatrix RepresentationPossible Code for This RepresentationAdjacency List Representation

3 Traversing the GraphBreadth-first searchDepth-First Search

4 ApplicationsFinding a path between nodesConnected ComponentsSpanning TreesTopological Sorting

19 / 91

Page 32: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Adjacency Matrix Representation

This is the simplest oneIf we number the nodes of an undirected graph:

20 / 91

Page 33: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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??!!!

21 / 91

Page 34: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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??!!!

21 / 91

Page 35: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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

22 / 91

Page 36: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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

22 / 91

Page 37: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

We have then...

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

23 / 91

Page 38: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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

24 / 91

Page 39: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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

25 / 91

Page 40: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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

25 / 91

Page 41: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

What about direct Graphs!!!

Similar ideaUse a 0 for no-edgeUse a 1 for directed edge

26 / 91

Page 42: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Example

We have that

3

1 2

4 5

6 7

1 2 3 4 5 6 71 0 1 1 1 0 0 02 0 0 0 1 1 0 03 0 0 0 0 0 1 04 0 0 1 0 0 1 15 0 0 0 1 0 0 16 0 0 0 0 0 0 07 0 0 0 0 0 1 0

27 / 91

Page 43: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Outline1 Graphs

Graphs EverywhereHistoryBasic Theory

2 Graph RepresentationMatrix RepresentationPossible Code for This RepresentationAdjacency List Representation

3 Traversing the GraphBreadth-first searchDepth-First Search

4 ApplicationsFinding a path between nodesConnected ComponentsSpanning TreesTopological Sorting

28 / 91

Page 44: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

What about the code?Partial Codep u b l i c c l a s s Graph{

// P r i v a t e s t o r a g ep r i v a t e boo l ean a d j a c e n c y M a t r i x [ ] [ ] ;p r i v a t e i n t c e r t e xCount ;

// C o n s t r u c t o rp u b l i c Graph ( i n t ve r t exCount ) {

t h i s . v e r t exCount = ve r t exCount ;a d j a c e n c y M a t r i x = new

boo l ean [ ve r t exCount ] [ v e r t exCount ] ;}

//Some Methodsp u b l i c v o i d addEdge ( i n t i , i n t j ) {

i f ( i >= 0 && i < ve r t exCount && j > 0&& j < ve r t exCount ) {

a d j a c e n c y M a t r i x [ i ] [ j ] = t r u e ;a d j a c e n c y M a t r i x [ j ] [ i ] = t r u e ;}

}p u b l i c v o i d removeEdge ( i n t i , i n t j ) {

i f ( i >= 0 && i < ve r t exCount && j > 0&& j < ve r t exCount ) {

a d j a c e n c y M a t r i x [ i ] [ j ] = f a l s e ;a d j a c e n c y M a t r i x [ j ] [ i ] = f a l s e ;

}}

}

29 / 91

Page 45: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

What about Time Complexity?

Operations on a GraphMost of the basic operations in a graph are:

Adding an edge – O(1)

Deleting an edge – O(1)

Answering the question “is there an edge between i and j” – O(1)

Finding the successors of a given vertex – O(N)

Finding (if exists) a path between two vertices – O(N2)

30 / 91

Page 46: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

What about Time Complexity?

Operations on a GraphMost of the basic operations in a graph are:

Adding an edge – O(1)

Deleting an edge – O(1)

Answering the question “is there an edge between i and j” – O(1)

Finding the successors of a given vertex – O(N)

Finding (if exists) a path between two vertices – O(N2)

30 / 91

Page 47: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

What about Time Complexity?

Operations on a GraphMost of the basic operations in a graph are:

Adding an edge – O(1)

Deleting an edge – O(1)

Answering the question “is there an edge between i and j” – O(1)

Finding the successors of a given vertex – O(N)

Finding (if exists) a path between two vertices – O(N2)

30 / 91

Page 48: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

What about Time Complexity?

Operations on a GraphMost of the basic operations in a graph are:

Adding an edge – O(1)

Deleting an edge – O(1)

Answering the question “is there an edge between i and j” – O(1)

Finding the successors of a given vertex – O(N)

Finding (if exists) a path between two vertices – O(N2)

30 / 91

Page 49: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

What about Time Complexity?

Operations on a GraphMost of the basic operations in a graph are:

Adding an edge – O(1)

Deleting an edge – O(1)

Answering the question “is there an edge between i and j” – O(1)

Finding the successors of a given vertex – O(N)

Finding (if exists) a path between two vertices – O(N2)

30 / 91

Page 50: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

What about Time Complexity?

Operations on a GraphMost of the basic operations in a graph are:

Adding an edge – O(1)

Deleting an edge – O(1)

Answering the question “is there an edge between i and j” – O(1)

Finding the successors of a given vertex – O(N)

Finding (if exists) a path between two vertices – O(N2)

30 / 91

Page 51: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

What about Time Complexity?

Operations on a GraphMost of the basic operations in a graph are:

Adding an edge – O(1)

Deleting an edge – O(1)

Answering the question “is there an edge between i and j” – O(1)

Finding the successors of a given vertex – O(N)

Finding (if exists) a path between two vertices – O(N2)

30 / 91

Page 52: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Space Drawbacks of This Representation

We need the following amount of spaceIf you have N integers of 4 bytes each, we requiere

4× N × N = 4N2

space

Which is a killer!!!If your graph does not have an edge between any two pair of nodes

SoWhat to do?

31 / 91

Page 53: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Space Drawbacks of This Representation

We need the following amount of spaceIf you have N integers of 4 bytes each, we requiere

4× N × N = 4N2

space

Which is a killer!!!If your graph does not have an edge between any two pair of nodes

SoWhat to do?

31 / 91

Page 54: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Space Drawbacks of This Representation

We need the following amount of spaceIf you have N integers of 4 bytes each, we requiere

4× N × N = 4N2

space

Which is a killer!!!If your graph does not have an edge between any two pair of nodes

SoWhat to do?

31 / 91

Page 55: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Possible Solutions

If you have an undirected graphFor an undirected graph, may store only lower or upper triangle(exclude diagonal).Space used (Assume Integers): 4× N(N−1)

2 = 2N (N − 1)

BetterUse Sparse Matrix Representations

32 / 91

Page 56: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Possible Solutions

If you have an undirected graphFor an undirected graph, may store only lower or upper triangle(exclude diagonal).Space used (Assume Integers): 4× N(N−1)

2 = 2N (N − 1)

BetterUse Sparse Matrix Representations

32 / 91

Page 57: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Possible Solutions

If you have an undirected graphFor an undirected graph, may store only lower or upper triangle(exclude diagonal).Space used (Assume Integers): 4× N(N−1)

2 = 2N (N − 1)

BetterUse Sparse Matrix Representations

32 / 91

Page 58: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

We use the sparse Representation of Matrices!!!

Example: Array of Row Chains

33 / 91

Page 59: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

We use the sparse Representation of Matrices!!!

Example: Orthogonal Lists

34 / 91

Page 60: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Code For Sparse Representation

Node and Link Representationsp u b l i c c l a s s Node {

p r i v a t e Node next ;p r i v a t e i n t c o l ;

p r i v a t e i n t Value

// Node c o n s t r u c t o rp u b l i c Node ( ){nex t . . .c o l . . .Va lue . . . }// Here the e x t r a// P u b l i c methods. . .

}p u b l i c c l a s s L i n k e d L i s t {

p r i v a t e Node head ;p r i v a t e i n t l i s t C o u n t ;

// L i n k e d L i s t c o n s t r u c t o rp u b l i c L i n k e d L i s t ( ) {head = new Node ( n u l l ) ;l i s t C o u n t = 0 ;}

35 / 91

Page 61: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Code For Sparse Representation

Graph Representationp u b l i c c l a s s G r a p h A d j L i s t S p a r s e {p u b l i c i n t NumNodes ;p u b l i c L i n k L i s t [ ] graph ;

// c o n s t r u c tp u b l i c G r a p h A d j L i s t S p a r s e ( i n t n ) {

NumNodes = n ;graph = new L i n k L i s t [ NumNodes ] ;f o r ( i n t i = 0 ; i < nodes ; i ++)

graph [ i ] = new L i n k L i s t ( ) ;}

// More Methods Here. . .

36 / 91

Page 62: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Outline1 Graphs

Graphs EverywhereHistoryBasic Theory

2 Graph RepresentationMatrix RepresentationPossible Code for This RepresentationAdjacency List Representation

3 Traversing the GraphBreadth-first searchDepth-First Search

4 ApplicationsFinding a path between nodesConnected ComponentsSpanning TreesTopological Sorting

37 / 91

Page 63: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Adjacency List Representation

DefinitionAdjacency list for vertex i is a linear list of vertices adjacent from vertex i .

BasicallyAn array of N adjacency lists.

ThusEach adjacency list is a chain.

38 / 91

Page 64: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Adjacency List Representation

DefinitionAdjacency list for vertex i is a linear list of vertices adjacent from vertex i .

BasicallyAn array of N adjacency lists.

ThusEach adjacency list is a chain.

38 / 91

Page 65: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Adjacency List Representation

DefinitionAdjacency list for vertex i is a linear list of vertices adjacent from vertex i .

BasicallyAn array of N adjacency lists.

ThusEach adjacency list is a chain.

38 / 91

Page 66: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Adjacency List Representation

For the previous example

1

2

3

4

5

6

4

4 6

4

5

1

2

2 3 5

39 / 91

Page 67: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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

40 / 91

Page 68: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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

40 / 91

Page 69: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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

40 / 91

Page 70: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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

40 / 91

Page 71: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Possible Disadvantage

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

41 / 91

Page 72: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Traversing the Graph

Why?Do you have any examples?

YesSearch for paths satisfying various constraints

I Shortest Path

Visit some sets of verticesI Tours

Search for subgraphsI Isomorphisms

42 / 91

Page 73: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Traversing the Graph

Why?Do you have any examples?

YesSearch for paths satisfying various constraints

I Shortest Path

Visit some sets of verticesI Tours

Search for subgraphsI Isomorphisms

42 / 91

Page 74: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Traversing the Graph

Why?Do you have any examples?

YesSearch for paths satisfying various constraints

I Shortest Path

Visit some sets of verticesI Tours

Search for subgraphsI Isomorphisms

42 / 91

Page 75: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Traversing the Graph

Why?Do you have any examples?

YesSearch for paths satisfying various constraints

I Shortest Path

Visit some sets of verticesI Tours

Search for subgraphsI Isomorphisms

42 / 91

Page 76: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Traversing the Graph

Why?Do you have any examples?

YesSearch for paths satisfying various constraints

I Shortest Path

Visit some sets of verticesI Tours

Search for subgraphsI Isomorphisms

42 / 91

Page 77: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Traversing the Graph

Why?Do you have any examples?

YesSearch for paths satisfying various constraints

I Shortest Path

Visit some sets of verticesI Tours

Search for subgraphsI Isomorphisms

42 / 91

Page 78: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Traversing the Graph

Why?Do you have any examples?

YesSearch for paths satisfying various constraints

I Shortest Path

Visit some sets of verticesI Tours

Search for subgraphsI Isomorphisms

42 / 91

Page 79: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Outline1 Graphs

Graphs EverywhereHistoryBasic Theory

2 Graph RepresentationMatrix RepresentationPossible Code for This RepresentationAdjacency List Representation

3 Traversing the GraphBreadth-first searchDepth-First Search

4 ApplicationsFinding a path between nodesConnected ComponentsSpanning TreesTopological Sorting

43 / 91

Page 80: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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

44 / 91

Page 81: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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

44 / 91

Page 82: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Defining Some Basic Terms

ColorGiven a node u, we have that

I color is a field indicatingF WHITE Never visited nodeF GRAY Node pointer is at the Queue QF BLACK Node has been visited and processed

45 / 91

Page 83: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Defining Some Basic Terms

ColorGiven a node u, we have that

I color is a field indicatingF WHITE Never visited nodeF GRAY Node pointer is at the Queue QF BLACK Node has been visited and processed

45 / 91

Page 84: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Defining Some Basic Terms

ColorGiven a node u, we have that

I color is a field indicatingF WHITE Never visited nodeF GRAY Node pointer is at the Queue QF BLACK Node has been visited and processed

45 / 91

Page 85: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Defining Some Basic Terms

ColorGiven a node u, we have that

I color is a field indicatingF WHITE Never visited nodeF GRAY Node pointer is at the Queue QF BLACK Node has been visited and processed

45 / 91

Page 86: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Defining Some Basic Terms

ColorGiven a node u, we have that

I color is a field indicatingF WHITE Never visited nodeF GRAY Node pointer is at the Queue QF BLACK Node has been visited and processed

45 / 91

Page 87: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Defining Some Basic Terms

Distance dGiven a node u, we have that

I d is a field indicating the distance from the node source s so far

46 / 91

Page 88: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Defining Some Basic Terms

Predecesor π

Given a node u, we have thatI π is a field indicating who is the immediate predecessor of u in the

path from s to u

47 / 91

Page 89: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Pseudo CodeBFS(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 = ∅11 u =Dequeue(Q)

12 for each v ∈ G.Adj [u]13 if v .color ==WHITE14 v .color =GRAY15 v .d = u.d + 116 Enqueue(Q, v)17 u.color = BLACK

48 / 91

Page 90: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Pseudo CodeBFS(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 = ∅11 u =Dequeue(Q)

12 for each v ∈ G.Adj [u]13 if v .color ==WHITE14 v .color =GRAY15 v .d = u.d + 116 Enqueue(Q, v)17 u.color = BLACK

48 / 91

Page 91: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Pseudo CodeBFS(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 = ∅11 u =Dequeue(Q)

12 for each v ∈ G.Adj [u]13 if v .color ==WHITE14 v .color =GRAY15 v .d = u.d + 116 Enqueue(Q, v)17 u.color = BLACK

48 / 91

Page 92: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Pseudo CodeBFS(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 = ∅11 u =Dequeue(Q)

12 for each v ∈ G.Adj [u]13 if v .color ==WHITE14 v .color =GRAY15 v .d = u.d + 116 Enqueue(Q, v)17 u.color = BLACK

48 / 91

Page 93: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Pseudo CodeBFS(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 = ∅11 u =Dequeue(Q)

12 for each v ∈ G.Adj [u]13 if v .color ==WHITE14 v .color =GRAY15 v .d = u.d + 116 Enqueue(Q, v)17 u.color = BLACK

48 / 91

Page 94: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Pseudo CodeBFS(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 = ∅11 u =Dequeue(Q)

12 for each v ∈ G.Adj [u]13 if v .color ==WHITE14 v .color =GRAY15 v .d = u.d + 116 Enqueue(Q, v)17 u.color = BLACK

48 / 91

Page 95: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Pseudo CodeBFS(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 = ∅11 u =Dequeue(Q)

12 for each v ∈ G.Adj [u]13 if v .color ==WHITE14 v .color =GRAY15 v .d = u.d + 116 Enqueue(Q, v)17 u.color = BLACK

48 / 91

Page 96: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Pseudo CodeBFS(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 = ∅11 u =Dequeue(Q)

12 for each v ∈ G.Adj [u]13 if v .color ==WHITE14 v .color =GRAY15 v .d = u.d + 116 Enqueue(Q, v)17 u.color = BLACK

48 / 91

Page 97: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Pseudo CodeBFS(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 = ∅11 u =Dequeue(Q)

12 for each v ∈ G.Adj [u]13 if v .color ==WHITE14 v .color =GRAY15 v .d = u.d + 116 Enqueue(Q, v)17 u.color = BLACK

48 / 91

Page 98: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Pseudo CodeBFS(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 = ∅11 u =Dequeue(Q)

12 for each v ∈ G.Adj [u]13 if v .color ==WHITE14 v .color =GRAY15 v .d = u.d + 116 Enqueue(Q, v)17 u.color = BLACK

48 / 91

Page 99: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Pseudo CodeBFS(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 = ∅11 u =Dequeue(Q)

12 for each v ∈ G.Adj [u]13 if v .color ==WHITE14 v .color =GRAY15 v .d = u.d + 116 Enqueue(Q, v)17 u.color = BLACK

48 / 91

Page 100: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Pseudo CodeBFS(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 = ∅11 u =Dequeue(Q)

12 for each v ∈ G.Adj [u]13 if v .color ==WHITE14 v .color =GRAY15 v .d = u.d + 116 Enqueue(Q, v)17 u.color = BLACK

48 / 91

Page 101: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Pseudo CodeBFS(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 = ∅11 u =Dequeue(Q)

12 for each v ∈ G.Adj [u]13 if v .color ==WHITE14 v .color =GRAY15 v .d = u.d + 116 Enqueue(Q, v)17 u.color = BLACK

48 / 91

Page 102: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Pseudo CodeBFS(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 = ∅11 u =Dequeue(Q)

12 for each v ∈ G.Adj [u]13 if v .color ==WHITE14 v .color =GRAY15 v .d = u.d + 116 Enqueue(Q, v)17 u.color = BLACK

48 / 91

Page 103: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Pseudo CodeBFS(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 = ∅11 u =Dequeue(Q)

12 for each v ∈ G.Adj [u]13 if v .color ==WHITE14 v .color =GRAY15 v .d = u.d + 116 Enqueue(Q, v)17 u.color = BLACK

48 / 91

Page 104: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Pseudo CodeBFS(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 = ∅11 u =Dequeue(Q)

12 for each v ∈ G.Adj [u]13 if v .color ==WHITE14 v .color =GRAY15 v .d = u.d + 116 Enqueue(Q, v)17 u.color = BLACK

48 / 91

Page 105: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Pseudo CodeBFS(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 = ∅11 u =Dequeue(Q)

12 for each v ∈ G.Adj [u]13 if v .color ==WHITE14 v .color =GRAY15 v .d = u.d + 116 Enqueue(Q, v)17 u.color = BLACK

48 / 91

Page 106: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Change the Order of Recursion

It is like a wave going from a node

1

2

3 4

5

6

49 / 91

Page 107: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Example

What do you see?

50 / 91

Page 108: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Example

What do you see?

51 / 91

Page 109: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Example

What do you see?

52 / 91

Page 110: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Example

What do you see?

53 / 91

Page 111: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Example

What do you see?

54 / 91

Page 112: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Example

What do you see?

55 / 91

Page 113: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Example

What do you see?

56 / 91

Page 114: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Example

What do you see?

57 / 91

Page 115: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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 )

58 / 91

Page 116: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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 )

58 / 91

Page 117: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Complexity

Overhead of CreationO (V )

ThenTotal complexity O (V + E )

59 / 91

Page 118: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Complexity

Overhead of CreationO (V )

ThenTotal complexity O (V + E )

59 / 91

Page 119: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Properties

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.

60 / 91

Page 120: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Properties

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.

60 / 91

Page 121: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Breadth-First Search Property

Something NotableAll vertices reachable from the start vertex (including the start vertex) arevisited.

61 / 91

Page 122: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Outline1 Graphs

Graphs EverywhereHistoryBasic Theory

2 Graph RepresentationMatrix RepresentationPossible Code for This RepresentationAdjacency List Representation

3 Traversing the GraphBreadth-first searchDepth-First Search

4 ApplicationsFinding a path between nodesConnected ComponentsSpanning TreesTopological Sorting

62 / 91

Page 123: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Depth-first search

Given GPick an unvisited vertex v, remember the rest.

I Recurse on vertices adjacent to v

63 / 91

Page 124: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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)

64 / 91

Page 125: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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)

64 / 91

Page 126: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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)

64 / 91

Page 127: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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)

64 / 91

Page 128: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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)

64 / 91

Page 129: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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)

64 / 91

Page 130: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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)

64 / 91

Page 131: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Now, we have the following

Code for DFS-VISITDFS-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 + 110 u.f = time

65 / 91

Page 132: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Now, we have the following

Code for DFS-VISITDFS-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 + 110 u.f = time

65 / 91

Page 133: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Now, we have the following

Code for DFS-VISITDFS-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 + 110 u.f = time

65 / 91

Page 134: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Now, we have the following

Code for DFS-VISITDFS-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 + 110 u.f = time

65 / 91

Page 135: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Now, we have the following

Code for DFS-VISITDFS-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 + 110 u.f = time

65 / 91

Page 136: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Now, we have the following

Code for DFS-VISITDFS-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 + 110 u.f = time

65 / 91

Page 137: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Now, we have the following

Code for DFS-VISITDFS-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 + 110 u.f = time

65 / 91

Page 138: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Now, we have the following

Code for DFS-VISITDFS-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 + 110 u.f = time

65 / 91

Page 139: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Now, we have the following

Code for DFS-VISITDFS-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 + 110 u.f = time

65 / 91

Page 140: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Now, we have the following

Code for DFS-VISITDFS-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 + 110 u.f = time

65 / 91

Page 141: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Example

What do we do?

66 / 91

Page 142: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Example

What do we do?

67 / 91

Page 143: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Example

What do we do?

68 / 91

Page 144: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Example

What do we do?

69 / 91

Page 145: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Example

What do we do?

70 / 91

Page 146: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Example

What do we do?

71 / 91

Page 147: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Example

What do we do?

72 / 91

Page 148: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Example

What do we do?

73 / 91

Page 149: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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)| = O (E ) we have that the cost of executing g

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

ThenDFS complexity is Θ (V + E )

74 / 91

Page 150: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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)| = O (E ) we have that the cost of executing g

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

ThenDFS complexity is Θ (V + E )

74 / 91

Page 151: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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)| = O (E ) we have that the cost of executing g

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

ThenDFS complexity is Θ (V + E )

74 / 91

Page 152: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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)| = O (E ) we have that the cost of executing g

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

ThenDFS complexity is Θ (V + E )

74 / 91

Page 153: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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)| = O (E ) we have that the cost of executing g

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

ThenDFS complexity is Θ (V + E )

74 / 91

Page 154: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Applications

We have severalFinding a path between nodesStrongly Connected ComponentsSpanning TreesTopological Sort - The Program (or Project) Evaluation and Review(PERT)Computer Vision AlgorithmsArtificial Intelligence AlgorithmsImportance in Social NetworkRank Algorithms for GoogleEtc.

75 / 91

Page 155: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Applications

We have severalFinding a path between nodesStrongly Connected ComponentsSpanning TreesTopological Sort - The Program (or Project) Evaluation and Review(PERT)Computer Vision AlgorithmsArtificial Intelligence AlgorithmsImportance in Social NetworkRank Algorithms for GoogleEtc.

75 / 91

Page 156: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Applications

We have severalFinding a path between nodesStrongly Connected ComponentsSpanning TreesTopological Sort - The Program (or Project) Evaluation and Review(PERT)Computer Vision AlgorithmsArtificial Intelligence AlgorithmsImportance in Social NetworkRank Algorithms for GoogleEtc.

75 / 91

Page 157: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Applications

We have severalFinding a path between nodesStrongly Connected ComponentsSpanning TreesTopological Sort - The Program (or Project) Evaluation and Review(PERT)Computer Vision AlgorithmsArtificial Intelligence AlgorithmsImportance in Social NetworkRank Algorithms for GoogleEtc.

75 / 91

Page 158: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Applications

We have severalFinding a path between nodesStrongly Connected ComponentsSpanning TreesTopological Sort - The Program (or Project) Evaluation and Review(PERT)Computer Vision AlgorithmsArtificial Intelligence AlgorithmsImportance in Social NetworkRank Algorithms for GoogleEtc.

75 / 91

Page 159: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Applications

We have severalFinding a path between nodesStrongly Connected ComponentsSpanning TreesTopological Sort - The Program (or Project) Evaluation and Review(PERT)Computer Vision AlgorithmsArtificial Intelligence AlgorithmsImportance in Social NetworkRank Algorithms for GoogleEtc.

75 / 91

Page 160: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Applications

We have severalFinding a path between nodesStrongly Connected ComponentsSpanning TreesTopological Sort - The Program (or Project) Evaluation and Review(PERT)Computer Vision AlgorithmsArtificial Intelligence AlgorithmsImportance in Social NetworkRank Algorithms for GoogleEtc.

75 / 91

Page 161: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Applications

We have severalFinding a path between nodesStrongly Connected ComponentsSpanning TreesTopological Sort - The Program (or Project) Evaluation and Review(PERT)Computer Vision AlgorithmsArtificial Intelligence AlgorithmsImportance in Social NetworkRank Algorithms for GoogleEtc.

75 / 91

Page 162: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Applications

We have severalFinding a path between nodesStrongly Connected ComponentsSpanning TreesTopological Sort - The Program (or Project) Evaluation and Review(PERT)Computer Vision AlgorithmsArtificial Intelligence AlgorithmsImportance in Social NetworkRank Algorithms for GoogleEtc.

75 / 91

Page 163: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Outline1 Graphs

Graphs EverywhereHistoryBasic Theory

2 Graph RepresentationMatrix RepresentationPossible Code for This RepresentationAdjacency List Representation

3 Traversing the GraphBreadth-first searchDepth-First Search

4 ApplicationsFinding a path between nodesConnected ComponentsSpanning TreesTopological Sorting

76 / 91

Page 164: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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.

77 / 91

Page 165: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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.

77 / 91

Page 166: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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.

77 / 91

Page 167: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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.

77 / 91

Page 168: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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 hasdistance(v) = δ(s, v)

78 / 91

Page 169: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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 hasdistance(v) = δ(s, v)

78 / 91

Page 170: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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 hasdistance(v) = δ(s, v)

78 / 91

Page 171: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Outline1 Graphs

Graphs EverywhereHistoryBasic Theory

2 Graph RepresentationMatrix RepresentationPossible Code for This RepresentationAdjacency List Representation

3 Traversing the GraphBreadth-first searchDepth-First Search

4 ApplicationsFinding a path between nodesConnected ComponentsSpanning TreesTopological Sorting

79 / 91

Page 172: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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

80 / 91

Page 173: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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

80 / 91

Page 174: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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.

81 / 91

Page 175: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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.

81 / 91

Page 176: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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.

81 / 91

Page 177: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Time

O (V 2)

When adjacency matrix used

O(V + E )

When adjacency lists used (E is number of edges)

82 / 91

Page 178: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Time

O (V 2)

When adjacency matrix used

O(V + E )

When adjacency lists used (E is number of edges)

82 / 91

Page 179: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Outline1 Graphs

Graphs EverywhereHistoryBasic Theory

2 Graph RepresentationMatrix RepresentationPossible Code for This RepresentationAdjacency List Representation

3 Traversing the GraphBreadth-first searchDepth-First Search

4 ApplicationsFinding a path between nodesConnected ComponentsSpanning TreesTopological Sorting

83 / 91

Page 180: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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

Example

84 / 91

Page 181: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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

Example

84 / 91

Page 182: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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).

85 / 91

Page 183: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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).

85 / 91

Page 184: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Time

O (V 2)

When adjacency matrix used

O(V + E )

When adjacency lists used (E is number of edges)

86 / 91

Page 185: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Time

O (V 2)

When adjacency matrix used

O(V + E )

When adjacency lists used (E is number of edges)

86 / 91

Page 186: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Outline1 Graphs

Graphs EverywhereHistoryBasic Theory

2 Graph RepresentationMatrix RepresentationPossible Code for This RepresentationAdjacency List Representation

3 Traversing the GraphBreadth-first searchDepth-First Search

4 ApplicationsFinding a path between nodesConnected ComponentsSpanning TreesTopological Sorting

87 / 91

Page 187: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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).

88 / 91

Page 188: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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).

88 / 91

Page 189: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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.

89 / 91

Page 190: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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.

89 / 91

Page 191: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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.

89 / 91

Page 192: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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

90 / 91

Page 193: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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

90 / 91

Page 194: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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

90 / 91

Page 195: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

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

90 / 91

Page 196: Preparation Data Structures 11 graphs

Images/cinvestav-1.jpg

Example

Dressing

91 / 91