Here are my slides for my preparation class for possible students in the Master in Electrical Engineering and Computer Science (Specialization in Computer Science)... for the entrance examination here at Cinvestav GDL.
2. Images/cinvestav-
Outline
1 Graphs
Graphs Everywhere
History
Basic Theory
2 Graph Representation
Matrix Representation
Possible Code for This Representation
Adjacency List Representation
3 Traversing the Graph
Breadth-first search
Depth-First Search
4 Applications
Finding a path between nodes
Connected Components
Spanning Trees
Topological Sorting
2 / 91
3. Images/cinvestav-
Outline
1 Graphs
Graphs Everywhere
History
Basic Theory
2 Graph Representation
Matrix Representation
Possible Code for This Representation
Adjacency List Representation
3 Traversing the Graph
Breadth-first search
Depth-First Search
4 Applications
Finding a path between nodes
Connected Components
Spanning Trees
Topological Sorting
3 / 91
7. Images/cinvestav-
Outline
1 Graphs
Graphs Everywhere
History
Basic Theory
2 Graph Representation
Matrix Representation
Possible Code for This Representation
Adjacency List Representation
3 Traversing the Graph
Breadth-first search
Depth-First Search
4 Applications
Finding a path between nodes
Connected Components
Spanning Trees
Topological Sorting
7 / 91
12. Images/cinvestav-
Necessary Condition
Euler discovered that
A necessary condition for the walk of the desired form is that the graph be
connected and have exactly zero or two nodes of odd degree.
Add an extra bridge
10 / 91
13. Images/cinvestav-
Necessary Condition
Euler discovered that
A necessary condition for the walk of the desired form is that the graph be
connected and have exactly zero or two nodes of odd degree.
Add an extra bridge
Add Extra Bridge
10 / 91
14. Images/cinvestav-
Studying Graphs
All the previous examples are telling us
Data Structures are required to design structures to hold the information
coming from graphs!!!
Good Representations
They will allow to handle the data structures with ease!!!
11 / 91
15. Images/cinvestav-
Studying Graphs
All the previous examples are telling us
Data Structures are required to design structures to hold the information
coming from graphs!!!
Good Representations
They will allow to handle the data structures with ease!!!
11 / 91
16. Images/cinvestav-
Outline
1 Graphs
Graphs Everywhere
History
Basic Theory
2 Graph Representation
Matrix Representation
Possible Code for This Representation
Adjacency List Representation
3 Traversing the Graph
Breadth-first search
Depth-First Search
4 Applications
Finding a path between nodes
Connected Components
Spanning Trees
Topological Sorting
12 / 91
17. Images/cinvestav-
Basic Theory
Definition
A Graph is composed of the following parts: Nodes and Edges
Nodes
They can represent multiple things:
People
Cities
States of Being
etc
Edges
They can represent multiple things too:
Distance between cities
Friendships
Matching Strings
Etc
13 / 91
18. Images/cinvestav-
Basic Theory
Definition
A Graph is composed of the following parts: Nodes and Edges
Nodes
They can represent multiple things:
People
Cities
States of Being
etc
Edges
They can represent multiple things too:
Distance between cities
Friendships
Matching Strings
Etc
13 / 91
19. Images/cinvestav-
Basic Theory
Definition
A Graph is composed of the following parts: Nodes and Edges
Nodes
They can represent multiple things:
People
Cities
States of Being
etc
Edges
They can represent multiple things too:
Distance between cities
Friendships
Matching Strings
Etc
13 / 91
20. Images/cinvestav-
Basic Theory
Definition
A graph G = (V , E) consists of a set of vertices (or nodes) V and a set of
edges E, where we assume that they are finite i.e. |V | = n and |E| = m.
Example
14 / 91
21. Images/cinvestav-
Basic Theory
Definition
A graph G = (V , E) consists of a set of vertices (or nodes) V and a set of
edges E, where we assume that they are finite i.e. |V | = n and |E| = m.
Example
14 / 91
24. Images/cinvestav-
Some properties
Degree
The degree d(v) of a vertex V is its number of incident edges
A self loop
A self-loop counts for 2 in the degree function.
Proposition
The sum of the degrees of a graph G = (V , E) equals 2|E| = 2m (trivial).
16 / 91
25. Images/cinvestav-
Some properties
Degree
The degree d(v) of a vertex V is its number of incident edges
A self loop
A self-loop counts for 2 in the degree function.
Proposition
The sum of the degrees of a graph G = (V , E) equals 2|E| = 2m (trivial).
16 / 91
26. Images/cinvestav-
Some properties
Degree
The degree d(v) of a vertex V is its number of incident edges
A self loop
A self-loop counts for 2 in the degree function.
Proposition
The sum of the degrees of a graph G = (V , E) equals 2|E| = 2m (trivial).
16 / 91
29. Images/cinvestav-
We need NICE representations
First One
Matrix Representation
Second One
Adjacency Representation
18 / 91
30. Images/cinvestav-
We need NICE representations
First One
Matrix Representation
Second One
Adjacency Representation
18 / 91
31. Images/cinvestav-
Outline
1 Graphs
Graphs Everywhere
History
Basic Theory
2 Graph Representation
Matrix Representation
Possible Code for This Representation
Adjacency List Representation
3 Traversing the Graph
Breadth-first search
Depth-First Search
4 Applications
Finding a path between nodes
Connected Components
Spanning Trees
Topological Sorting
19 / 91
33. Images/cinvestav-
Adjacency Matrix Representation
In a natural way the edges can be identified by the nodes
For example, the edge between 1 and 4 nodes gets named as (1,4)
Then
How, we use this to represent the graph through a Matrix or and Array of
Arrays??!!!
21 / 91
34. Images/cinvestav-
Adjacency Matrix Representation
In a natural way the edges can be identified by the nodes
For example, the edge between 1 and 4 nodes gets named as (1,4)
Then
How, we use this to represent the graph through a Matrix or and Array of
Arrays??!!!
21 / 91
35. Images/cinvestav-
What about the following?
How do we indicate that an edge exist given the following matrix
1 2 3 4 5 6
1 − − − − − −
2 − − − − − −
3 − − − − − −
4 − − − − − −
5 − − − − − −
6 − − − − − −
You say it!!
Use a 0 for no-edge
Use a 1 for edge
22 / 91
36. Images/cinvestav-
What about the following?
How do we indicate that an edge exist given the following matrix
1 2 3 4 5 6
1 − − − − − −
2 − − − − − −
3 − − − − − −
4 − − − − − −
5 − − − − − −
6 − − − − − −
You say it!!
Use a 0 for no-edge
Use a 1 for edge
22 / 91
39. Images/cinvestav-
Properties of the Matrix for Undirected Graphs
Property One
Diagonal entries are zero.
Property Two
Adjacency matrix of an undirected graph is symmetric:
A (i, j) = A (j, i) for all i and j
25 / 91
40. Images/cinvestav-
Properties of the Matrix for Undirected Graphs
Property One
Diagonal entries are zero.
Property Two
Adjacency matrix of an undirected graph is symmetric:
A (i, j) = A (j, i) for all i and j
25 / 91
43. Images/cinvestav-
Outline
1 Graphs
Graphs Everywhere
History
Basic Theory
2 Graph Representation
Matrix Representation
Possible Code for This Representation
Adjacency List Representation
3 Traversing the Graph
Breadth-first search
Depth-First Search
4 Applications
Finding a path between nodes
Connected Components
Spanning Trees
Topological Sorting
28 / 91
44. Images/cinvestav-
What about the code?
Partial Code
p u b l i c c l a s s Graph{
// P r i v a t e s t o r a g e
p r i v a t e boolean adjacencyMatrix [ ] [ ] ;
p r i v a t e i n t certexCount ;
// Co nstruct or
p u b l i c Graph ( i n t vertexCount ) {
t h i s . vertexCount = vertexCount ;
adjacencyMatrix = new
boolean [ vertexCount ] [ vertexCount ] ;
}
//Some Methods
p u b l i c void addEdge ( i n t i , i n t j ) {
i f ( i >= 0 && i < vertexCount && j > 0
&& j < vertexCount ) {
adjacencyMatrix [ i ] [ j ] = t r u e ;
adjacencyMatrix [ j ] [ i ] = t r u e ;
}
}
p u b l i c void removeEdge ( i n t i , i n t j ) {
i f ( i >= 0 && i < vertexCount && j > 0
&& j < vertexCount ) {
adjacencyMatrix [ i ] [ j ] = f a l s e ;
adjacencyMatrix [ j ] [ i ] = f a l s e ;
}
}
}
29 / 91
45. Images/cinvestav-
What about Time Complexity?
Operations on a Graph
Most 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
46. Images/cinvestav-
What about Time Complexity?
Operations on a Graph
Most 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
47. Images/cinvestav-
What about Time Complexity?
Operations on a Graph
Most 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
48. Images/cinvestav-
What about Time Complexity?
Operations on a Graph
Most 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
49. Images/cinvestav-
What about Time Complexity?
Operations on a Graph
Most 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
50. Images/cinvestav-
What about Time Complexity?
Operations on a Graph
Most 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
51. Images/cinvestav-
What about Time Complexity?
Operations on a Graph
Most 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
52. Images/cinvestav-
Space Drawbacks of This Representation
We need the following amount of space
If 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
So
What to do?
31 / 91
53. Images/cinvestav-
Space Drawbacks of This Representation
We need the following amount of space
If 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
So
What to do?
31 / 91
54. Images/cinvestav-
Space Drawbacks of This Representation
We need the following amount of space
If 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
So
What to do?
31 / 91
55. Images/cinvestav-
Possible Solutions
If you have an undirected graph
For an undirected graph, may store only lower or upper triangle
(exclude diagonal).
Space used (Assume Integers): 4 × N(N−1)
2 = 2N (N − 1)
Better
Use Sparse Matrix Representations
32 / 91
56. Images/cinvestav-
Possible Solutions
If you have an undirected graph
For an undirected graph, may store only lower or upper triangle
(exclude diagonal).
Space used (Assume Integers): 4 × N(N−1)
2 = 2N (N − 1)
Better
Use Sparse Matrix Representations
32 / 91
57. Images/cinvestav-
Possible Solutions
If you have an undirected graph
For an undirected graph, may store only lower or upper triangle
(exclude diagonal).
Space used (Assume Integers): 4 × N(N−1)
2 = 2N (N − 1)
Better
Use Sparse Matrix Representations
32 / 91
60. Images/cinvestav-
Code For Sparse Representation
Node and Link Representations
p 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 r
p u b l i c Node (){
next . . .
c o l . . .
Value . . . }
// 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 r
p 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
61. Images/cinvestav-
Code For Sparse Representation
Graph Representation
p u b l i c c l a s s GraphAdjListSparse {
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 t
p u b l i c GraphAdjListSparse ( 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
62. Images/cinvestav-
Outline
1 Graphs
Graphs Everywhere
History
Basic Theory
2 Graph Representation
Matrix Representation
Possible Code for This Representation
Adjacency List Representation
3 Traversing the Graph
Breadth-first search
Depth-First Search
4 Applications
Finding a path between nodes
Connected Components
Spanning Trees
Topological Sorting
37 / 91
67. Images/cinvestav-
Properties
Space for storage
For undirected or directed graphs O (V + E)
Search: Successful or Unsuccessful
O(1 + degree(v))
In addition
Adjacency lists can readily be adapted to represent weighted graphs
Weight function w : E → R
The weight w(u, v) of the edge (u, v) ∈ E is simply stored with
vertex v in u’s adjacency list
40 / 91
68. Images/cinvestav-
Properties
Space for storage
For undirected or directed graphs O (V + E)
Search: Successful or Unsuccessful
O(1 + degree(v))
In addition
Adjacency lists can readily be adapted to represent weighted graphs
Weight function w : E → R
The weight w(u, v) of the edge (u, v) ∈ E is simply stored with
vertex v in u’s adjacency list
40 / 91
69. Images/cinvestav-
Properties
Space for storage
For undirected or directed graphs O (V + E)
Search: Successful or Unsuccessful
O(1 + degree(v))
In addition
Adjacency lists can readily be adapted to represent weighted graphs
Weight function w : E → R
The weight w(u, v) of the edge (u, v) ∈ E is simply stored with
vertex v in u’s adjacency list
40 / 91
70. Images/cinvestav-
Properties
Space for storage
For undirected or directed graphs O (V + E)
Search: Successful or Unsuccessful
O(1 + degree(v))
In addition
Adjacency lists can readily be adapted to represent weighted graphs
Weight function w : E → R
The weight w(u, v) of the edge (u, v) ∈ E is simply stored with
vertex v in u’s adjacency list
40 / 91
72. Images/cinvestav-
Traversing the Graph
Why?
Do you have any examples?
Yes
Search for paths satisfying various constraints
Shortest Path
Visit some sets of vertices
Tours
Search for subgraphs
Isomorphisms
42 / 91
73. Images/cinvestav-
Traversing the Graph
Why?
Do you have any examples?
Yes
Search for paths satisfying various constraints
Shortest Path
Visit some sets of vertices
Tours
Search for subgraphs
Isomorphisms
42 / 91
74. Images/cinvestav-
Traversing the Graph
Why?
Do you have any examples?
Yes
Search for paths satisfying various constraints
Shortest Path
Visit some sets of vertices
Tours
Search for subgraphs
Isomorphisms
42 / 91
75. Images/cinvestav-
Traversing the Graph
Why?
Do you have any examples?
Yes
Search for paths satisfying various constraints
Shortest Path
Visit some sets of vertices
Tours
Search for subgraphs
Isomorphisms
42 / 91
76. Images/cinvestav-
Traversing the Graph
Why?
Do you have any examples?
Yes
Search for paths satisfying various constraints
Shortest Path
Visit some sets of vertices
Tours
Search for subgraphs
Isomorphisms
42 / 91
77. Images/cinvestav-
Traversing the Graph
Why?
Do you have any examples?
Yes
Search for paths satisfying various constraints
Shortest Path
Visit some sets of vertices
Tours
Search for subgraphs
Isomorphisms
42 / 91
78. Images/cinvestav-
Traversing the Graph
Why?
Do you have any examples?
Yes
Search for paths satisfying various constraints
Shortest Path
Visit some sets of vertices
Tours
Search for subgraphs
Isomorphisms
42 / 91
79. Images/cinvestav-
Outline
1 Graphs
Graphs Everywhere
History
Basic Theory
2 Graph Representation
Matrix Representation
Possible Code for This Representation
Adjacency List Representation
3 Traversing the Graph
Breadth-first search
Depth-First Search
4 Applications
Finding a path between nodes
Connected Components
Spanning Trees
Topological Sorting
43 / 91
80. Images/cinvestav-
Breadth-first search
Definition
Given a graph G = (V , E) and a source vertex s, breadth-first search
systematically explores the edges of Gto “discover” every vertex that is
reachable from the vertex s
Something Notable
A vertex is discovered the first time it is encountered during the search
44 / 91
81. Images/cinvestav-
Breadth-first search
Definition
Given a graph G = (V , E) and a source vertex s, breadth-first search
systematically explores the edges of Gto “discover” every vertex that is
reachable from the vertex s
Something Notable
A vertex is discovered the first time it is encountered during the search
44 / 91
82. Images/cinvestav-
Defining Some Basic Terms
Color
Given a node u, we have that
color is a field indicating
WHITE Never visited node
GRAY Node pointer is at the Queue Q
BLACK Node has been visited and processed
45 / 91
83. Images/cinvestav-
Defining Some Basic Terms
Color
Given a node u, we have that
color is a field indicating
WHITE Never visited node
GRAY Node pointer is at the Queue Q
BLACK Node has been visited and processed
45 / 91
84. Images/cinvestav-
Defining Some Basic Terms
Color
Given a node u, we have that
color is a field indicating
WHITE Never visited node
GRAY Node pointer is at the Queue Q
BLACK Node has been visited and processed
45 / 91
85. Images/cinvestav-
Defining Some Basic Terms
Color
Given a node u, we have that
color is a field indicating
WHITE Never visited node
GRAY Node pointer is at the Queue Q
BLACK Node has been visited and processed
45 / 91
86. Images/cinvestav-
Defining Some Basic Terms
Color
Given a node u, we have that
color is a field indicating
WHITE Never visited node
GRAY Node pointer is at the Queue Q
BLACK Node has been visited and processed
45 / 91
87. Images/cinvestav-
Defining Some Basic Terms
Distance d
Given a node u, we have that
d is a field indicating the distance from the node source s so far
46 / 91
88. Images/cinvestav-
Defining Some Basic Terms
Predecesor π
Given a node u, we have that
π is a field indicating who is the immediate predecessor of u in the
path from s to u
47 / 91
89. Images/cinvestav-
Pseudo Code
BFS(G, s)
1 for each vertex u ∈ G.V − {s}
2 u.color = WHITE
3 u.d = ∞
4 u.π = NIL
5 s.color =GRAY
6 s.d = 0
7 s.π = NIL
8 Q = ∅
9 Enqueue(Q, s)
10 while Q = ∅
11 u =Dequeue(Q)
12 for each v ∈ G.Adj [u]
13 if v.color ==WHITE
14 v.color =GRAY
15 v.d = u.d + 1
16 Enqueue(Q, v)
17 u.color = BLACK
48 / 91
90. Images/cinvestav-
Pseudo Code
BFS(G, s)
1 for each vertex u ∈ G.V − {s}
2 u.color = WHITE
3 u.d = ∞
4 u.π = NIL
5 s.color =GRAY
6 s.d = 0
7 s.π = NIL
8 Q = ∅
9 Enqueue(Q, s)
10 while Q = ∅
11 u =Dequeue(Q)
12 for each v ∈ G.Adj [u]
13 if v.color ==WHITE
14 v.color =GRAY
15 v.d = u.d + 1
16 Enqueue(Q, v)
17 u.color = BLACK
48 / 91
91. Images/cinvestav-
Pseudo Code
BFS(G, s)
1 for each vertex u ∈ G.V − {s}
2 u.color = WHITE
3 u.d = ∞
4 u.π = NIL
5 s.color =GRAY
6 s.d = 0
7 s.π = NIL
8 Q = ∅
9 Enqueue(Q, s)
10 while Q = ∅
11 u =Dequeue(Q)
12 for each v ∈ G.Adj [u]
13 if v.color ==WHITE
14 v.color =GRAY
15 v.d = u.d + 1
16 Enqueue(Q, v)
17 u.color = BLACK
48 / 91
92. Images/cinvestav-
Pseudo Code
BFS(G, s)
1 for each vertex u ∈ G.V − {s}
2 u.color = WHITE
3 u.d = ∞
4 u.π = NIL
5 s.color =GRAY
6 s.d = 0
7 s.π = NIL
8 Q = ∅
9 Enqueue(Q, s)
10 while Q = ∅
11 u =Dequeue(Q)
12 for each v ∈ G.Adj [u]
13 if v.color ==WHITE
14 v.color =GRAY
15 v.d = u.d + 1
16 Enqueue(Q, v)
17 u.color = BLACK
48 / 91
93. Images/cinvestav-
Pseudo Code
BFS(G, s)
1 for each vertex u ∈ G.V − {s}
2 u.color = WHITE
3 u.d = ∞
4 u.π = NIL
5 s.color =GRAY
6 s.d = 0
7 s.π = NIL
8 Q = ∅
9 Enqueue(Q, s)
10 while Q = ∅
11 u =Dequeue(Q)
12 for each v ∈ G.Adj [u]
13 if v.color ==WHITE
14 v.color =GRAY
15 v.d = u.d + 1
16 Enqueue(Q, v)
17 u.color = BLACK
48 / 91
94. Images/cinvestav-
Pseudo Code
BFS(G, s)
1 for each vertex u ∈ G.V − {s}
2 u.color = WHITE
3 u.d = ∞
4 u.π = NIL
5 s.color =GRAY
6 s.d = 0
7 s.π = NIL
8 Q = ∅
9 Enqueue(Q, s)
10 while Q = ∅
11 u =Dequeue(Q)
12 for each v ∈ G.Adj [u]
13 if v.color ==WHITE
14 v.color =GRAY
15 v.d = u.d + 1
16 Enqueue(Q, v)
17 u.color = BLACK
48 / 91
95. Images/cinvestav-
Pseudo Code
BFS(G, s)
1 for each vertex u ∈ G.V − {s}
2 u.color = WHITE
3 u.d = ∞
4 u.π = NIL
5 s.color =GRAY
6 s.d = 0
7 s.π = NIL
8 Q = ∅
9 Enqueue(Q, s)
10 while Q = ∅
11 u =Dequeue(Q)
12 for each v ∈ G.Adj [u]
13 if v.color ==WHITE
14 v.color =GRAY
15 v.d = u.d + 1
16 Enqueue(Q, v)
17 u.color = BLACK
48 / 91
96. Images/cinvestav-
Pseudo Code
BFS(G, s)
1 for each vertex u ∈ G.V − {s}
2 u.color = WHITE
3 u.d = ∞
4 u.π = NIL
5 s.color =GRAY
6 s.d = 0
7 s.π = NIL
8 Q = ∅
9 Enqueue(Q, s)
10 while Q = ∅
11 u =Dequeue(Q)
12 for each v ∈ G.Adj [u]
13 if v.color ==WHITE
14 v.color =GRAY
15 v.d = u.d + 1
16 Enqueue(Q, v)
17 u.color = BLACK
48 / 91
97. Images/cinvestav-
Pseudo Code
BFS(G, s)
1 for each vertex u ∈ G.V − {s}
2 u.color = WHITE
3 u.d = ∞
4 u.π = NIL
5 s.color =GRAY
6 s.d = 0
7 s.π = NIL
8 Q = ∅
9 Enqueue(Q, s)
10 while Q = ∅
11 u =Dequeue(Q)
12 for each v ∈ G.Adj [u]
13 if v.color ==WHITE
14 v.color =GRAY
15 v.d = u.d + 1
16 Enqueue(Q, v)
17 u.color = BLACK
48 / 91
98. Images/cinvestav-
Pseudo Code
BFS(G, s)
1 for each vertex u ∈ G.V − {s}
2 u.color = WHITE
3 u.d = ∞
4 u.π = NIL
5 s.color =GRAY
6 s.d = 0
7 s.π = NIL
8 Q = ∅
9 Enqueue(Q, s)
10 while Q = ∅
11 u =Dequeue(Q)
12 for each v ∈ G.Adj [u]
13 if v.color ==WHITE
14 v.color =GRAY
15 v.d = u.d + 1
16 Enqueue(Q, v)
17 u.color = BLACK
48 / 91
99. Images/cinvestav-
Pseudo Code
BFS(G, s)
1 for each vertex u ∈ G.V − {s}
2 u.color = WHITE
3 u.d = ∞
4 u.π = NIL
5 s.color =GRAY
6 s.d = 0
7 s.π = NIL
8 Q = ∅
9 Enqueue(Q, s)
10 while Q = ∅
11 u =Dequeue(Q)
12 for each v ∈ G.Adj [u]
13 if v.color ==WHITE
14 v.color =GRAY
15 v.d = u.d + 1
16 Enqueue(Q, v)
17 u.color = BLACK
48 / 91
100. Images/cinvestav-
Pseudo Code
BFS(G, s)
1 for each vertex u ∈ G.V − {s}
2 u.color = WHITE
3 u.d = ∞
4 u.π = NIL
5 s.color =GRAY
6 s.d = 0
7 s.π = NIL
8 Q = ∅
9 Enqueue(Q, s)
10 while Q = ∅
11 u =Dequeue(Q)
12 for each v ∈ G.Adj [u]
13 if v.color ==WHITE
14 v.color =GRAY
15 v.d = u.d + 1
16 Enqueue(Q, v)
17 u.color = BLACK
48 / 91
101. Images/cinvestav-
Pseudo Code
BFS(G, s)
1 for each vertex u ∈ G.V − {s}
2 u.color = WHITE
3 u.d = ∞
4 u.π = NIL
5 s.color =GRAY
6 s.d = 0
7 s.π = NIL
8 Q = ∅
9 Enqueue(Q, s)
10 while Q = ∅
11 u =Dequeue(Q)
12 for each v ∈ G.Adj [u]
13 if v.color ==WHITE
14 v.color =GRAY
15 v.d = u.d + 1
16 Enqueue(Q, v)
17 u.color = BLACK
48 / 91
102. Images/cinvestav-
Pseudo Code
BFS(G, s)
1 for each vertex u ∈ G.V − {s}
2 u.color = WHITE
3 u.d = ∞
4 u.π = NIL
5 s.color =GRAY
6 s.d = 0
7 s.π = NIL
8 Q = ∅
9 Enqueue(Q, s)
10 while Q = ∅
11 u =Dequeue(Q)
12 for each v ∈ G.Adj [u]
13 if v.color ==WHITE
14 v.color =GRAY
15 v.d = u.d + 1
16 Enqueue(Q, v)
17 u.color = BLACK
48 / 91
103. Images/cinvestav-
Pseudo Code
BFS(G, s)
1 for each vertex u ∈ G.V − {s}
2 u.color = WHITE
3 u.d = ∞
4 u.π = NIL
5 s.color =GRAY
6 s.d = 0
7 s.π = NIL
8 Q = ∅
9 Enqueue(Q, s)
10 while Q = ∅
11 u =Dequeue(Q)
12 for each v ∈ G.Adj [u]
13 if v.color ==WHITE
14 v.color =GRAY
15 v.d = u.d + 1
16 Enqueue(Q, v)
17 u.color = BLACK
48 / 91
104. Images/cinvestav-
Pseudo Code
BFS(G, s)
1 for each vertex u ∈ G.V − {s}
2 u.color = WHITE
3 u.d = ∞
4 u.π = NIL
5 s.color =GRAY
6 s.d = 0
7 s.π = NIL
8 Q = ∅
9 Enqueue(Q, s)
10 while Q = ∅
11 u =Dequeue(Q)
12 for each v ∈ G.Adj [u]
13 if v.color ==WHITE
14 v.color =GRAY
15 v.d = u.d + 1
16 Enqueue(Q, v)
17 u.color = BLACK
48 / 91
105. Images/cinvestav-
Pseudo Code
BFS(G, s)
1 for each vertex u ∈ G.V − {s}
2 u.color = WHITE
3 u.d = ∞
4 u.π = NIL
5 s.color =GRAY
6 s.d = 0
7 s.π = NIL
8 Q = ∅
9 Enqueue(Q, s)
10 while Q = ∅
11 u =Dequeue(Q)
12 for each v ∈ G.Adj [u]
13 if v.color ==WHITE
14 v.color =GRAY
15 v.d = u.d + 1
16 Enqueue(Q, v)
17 u.color = BLACK
48 / 91
115. Images/cinvestav-
Complexity
What about the outer loop?
O(V ) Enqueue / Dequeue operations – Each adjacency list is processed
only once.
What about the inner loop?
The sum of the lengths of f all the adjacency lists is Θ(E) so the scanning
takes O(E)
58 / 91
116. Images/cinvestav-
Complexity
What about the outer loop?
O(V ) Enqueue / Dequeue operations – Each adjacency list is processed
only once.
What about the inner loop?
The sum of the lengths of f all the adjacency lists is Θ(E) so the scanning
takes O(E)
58 / 91
122. Images/cinvestav-
Outline
1 Graphs
Graphs Everywhere
History
Basic Theory
2 Graph Representation
Matrix Representation
Possible Code for This Representation
Adjacency List Representation
3 Traversing the Graph
Breadth-first search
Depth-First Search
4 Applications
Finding a path between nodes
Connected Components
Spanning Trees
Topological Sorting
62 / 91
124. Images/cinvestav-
The Pseudo-code
Code for DFS
DFS(G)
1 for each vertex u ∈ G.V
2 u.color = WHITE
3 u.π = NIL
4 time = 0
5 for each vertex u ∈ G.V
6 if u.color = WHITE
7 DFS-VISIT(G, u)
64 / 91
125. Images/cinvestav-
The Pseudo-code
Code for DFS
DFS(G)
1 for each vertex u ∈ G.V
2 u.color = WHITE
3 u.π = NIL
4 time = 0
5 for each vertex u ∈ G.V
6 if u.color = WHITE
7 DFS-VISIT(G, u)
64 / 91
126. Images/cinvestav-
The Pseudo-code
Code for DFS
DFS(G)
1 for each vertex u ∈ G.V
2 u.color = WHITE
3 u.π = NIL
4 time = 0
5 for each vertex u ∈ G.V
6 if u.color = WHITE
7 DFS-VISIT(G, u)
64 / 91
127. Images/cinvestav-
The Pseudo-code
Code for DFS
DFS(G)
1 for each vertex u ∈ G.V
2 u.color = WHITE
3 u.π = NIL
4 time = 0
5 for each vertex u ∈ G.V
6 if u.color = WHITE
7 DFS-VISIT(G, u)
64 / 91
128. Images/cinvestav-
The Pseudo-code
Code for DFS
DFS(G)
1 for each vertex u ∈ G.V
2 u.color = WHITE
3 u.π = NIL
4 time = 0
5 for each vertex u ∈ G.V
6 if u.color = WHITE
7 DFS-VISIT(G, u)
64 / 91
129. Images/cinvestav-
The Pseudo-code
Code for DFS
DFS(G)
1 for each vertex u ∈ G.V
2 u.color = WHITE
3 u.π = NIL
4 time = 0
5 for each vertex u ∈ G.V
6 if u.color = WHITE
7 DFS-VISIT(G, u)
64 / 91
130. Images/cinvestav-
The Pseudo-code
Code for DFS
DFS(G)
1 for each vertex u ∈ G.V
2 u.color = WHITE
3 u.π = NIL
4 time = 0
5 for each vertex u ∈ G.V
6 if u.color = WHITE
7 DFS-VISIT(G, u)
64 / 91
131. Images/cinvestav-
Now, we have the following
Code for DFS-VISIT
DFS-VISIT(G, u)
1 time = time + 1
2 u.d = time
3 u.color = GRAY
4 for each vertex v ∈ G.Adj [u]
5 if v.color == WHITE
6 v.π = u
7 DFS-VISIT(G, v)
8 u.color = BLACK
9 time = time + 1
10 u.f = time
65 / 91
132. Images/cinvestav-
Now, we have the following
Code for DFS-VISIT
DFS-VISIT(G, u)
1 time = time + 1
2 u.d = time
3 u.color = GRAY
4 for each vertex v ∈ G.Adj [u]
5 if v.color == WHITE
6 v.π = u
7 DFS-VISIT(G, v)
8 u.color = BLACK
9 time = time + 1
10 u.f = time
65 / 91
133. Images/cinvestav-
Now, we have the following
Code for DFS-VISIT
DFS-VISIT(G, u)
1 time = time + 1
2 u.d = time
3 u.color = GRAY
4 for each vertex v ∈ G.Adj [u]
5 if v.color == WHITE
6 v.π = u
7 DFS-VISIT(G, v)
8 u.color = BLACK
9 time = time + 1
10 u.f = time
65 / 91
134. Images/cinvestav-
Now, we have the following
Code for DFS-VISIT
DFS-VISIT(G, u)
1 time = time + 1
2 u.d = time
3 u.color = GRAY
4 for each vertex v ∈ G.Adj [u]
5 if v.color == WHITE
6 v.π = u
7 DFS-VISIT(G, v)
8 u.color = BLACK
9 time = time + 1
10 u.f = time
65 / 91
135. Images/cinvestav-
Now, we have the following
Code for DFS-VISIT
DFS-VISIT(G, u)
1 time = time + 1
2 u.d = time
3 u.color = GRAY
4 for each vertex v ∈ G.Adj [u]
5 if v.color == WHITE
6 v.π = u
7 DFS-VISIT(G, v)
8 u.color = BLACK
9 time = time + 1
10 u.f = time
65 / 91
136. Images/cinvestav-
Now, we have the following
Code for DFS-VISIT
DFS-VISIT(G, u)
1 time = time + 1
2 u.d = time
3 u.color = GRAY
4 for each vertex v ∈ G.Adj [u]
5 if v.color == WHITE
6 v.π = u
7 DFS-VISIT(G, v)
8 u.color = BLACK
9 time = time + 1
10 u.f = time
65 / 91
137. Images/cinvestav-
Now, we have the following
Code for DFS-VISIT
DFS-VISIT(G, u)
1 time = time + 1
2 u.d = time
3 u.color = GRAY
4 for each vertex v ∈ G.Adj [u]
5 if v.color == WHITE
6 v.π = u
7 DFS-VISIT(G, v)
8 u.color = BLACK
9 time = time + 1
10 u.f = time
65 / 91
138. Images/cinvestav-
Now, we have the following
Code for DFS-VISIT
DFS-VISIT(G, u)
1 time = time + 1
2 u.d = time
3 u.color = GRAY
4 for each vertex v ∈ G.Adj [u]
5 if v.color == WHITE
6 v.π = u
7 DFS-VISIT(G, v)
8 u.color = BLACK
9 time = time + 1
10 u.f = time
65 / 91
139. Images/cinvestav-
Now, we have the following
Code for DFS-VISIT
DFS-VISIT(G, u)
1 time = time + 1
2 u.d = time
3 u.color = GRAY
4 for each vertex v ∈ G.Adj [u]
5 if v.color == WHITE
6 v.π = u
7 DFS-VISIT(G, v)
8 u.color = BLACK
9 time = time + 1
10 u.f = time
65 / 91
140. Images/cinvestav-
Now, we have the following
Code for DFS-VISIT
DFS-VISIT(G, u)
1 time = time + 1
2 u.d = time
3 u.color = GRAY
4 for each vertex v ∈ G.Adj [u]
5 if v.color == WHITE
6 v.π = u
7 DFS-VISIT(G, v)
8 u.color = BLACK
9 time = time + 1
10 u.f = time
65 / 91
149. Images/cinvestav-
Complexity
Analysis
1 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) .
Then
DFS complexity is Θ (V + E)
74 / 91
150. Images/cinvestav-
Complexity
Analysis
1 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) .
Then
DFS complexity is Θ (V + E)
74 / 91
151. Images/cinvestav-
Complexity
Analysis
1 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) .
Then
DFS complexity is Θ (V + E)
74 / 91
152. Images/cinvestav-
Complexity
Analysis
1 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) .
Then
DFS complexity is Θ (V + E)
74 / 91
153. Images/cinvestav-
Complexity
Analysis
1 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) .
Then
DFS complexity is Θ (V + E)
74 / 91
154. Images/cinvestav-
Applications
We have several
Finding a path between nodes
Strongly Connected Components
Spanning Trees
Topological Sort - The Program (or Project) Evaluation and Review
(PERT)
Computer Vision Algorithms
Artificial Intelligence Algorithms
Importance in Social Network
Rank Algorithms for Google
Etc.
75 / 91
155. Images/cinvestav-
Applications
We have several
Finding a path between nodes
Strongly Connected Components
Spanning Trees
Topological Sort - The Program (or Project) Evaluation and Review
(PERT)
Computer Vision Algorithms
Artificial Intelligence Algorithms
Importance in Social Network
Rank Algorithms for Google
Etc.
75 / 91
156. Images/cinvestav-
Applications
We have several
Finding a path between nodes
Strongly Connected Components
Spanning Trees
Topological Sort - The Program (or Project) Evaluation and Review
(PERT)
Computer Vision Algorithms
Artificial Intelligence Algorithms
Importance in Social Network
Rank Algorithms for Google
Etc.
75 / 91
157. Images/cinvestav-
Applications
We have several
Finding a path between nodes
Strongly Connected Components
Spanning Trees
Topological Sort - The Program (or Project) Evaluation and Review
(PERT)
Computer Vision Algorithms
Artificial Intelligence Algorithms
Importance in Social Network
Rank Algorithms for Google
Etc.
75 / 91
158. Images/cinvestav-
Applications
We have several
Finding a path between nodes
Strongly Connected Components
Spanning Trees
Topological Sort - The Program (or Project) Evaluation and Review
(PERT)
Computer Vision Algorithms
Artificial Intelligence Algorithms
Importance in Social Network
Rank Algorithms for Google
Etc.
75 / 91
159. Images/cinvestav-
Applications
We have several
Finding a path between nodes
Strongly Connected Components
Spanning Trees
Topological Sort - The Program (or Project) Evaluation and Review
(PERT)
Computer Vision Algorithms
Artificial Intelligence Algorithms
Importance in Social Network
Rank Algorithms for Google
Etc.
75 / 91
160. Images/cinvestav-
Applications
We have several
Finding a path between nodes
Strongly Connected Components
Spanning Trees
Topological Sort - The Program (or Project) Evaluation and Review
(PERT)
Computer Vision Algorithms
Artificial Intelligence Algorithms
Importance in Social Network
Rank Algorithms for Google
Etc.
75 / 91
161. Images/cinvestav-
Applications
We have several
Finding a path between nodes
Strongly Connected Components
Spanning Trees
Topological Sort - The Program (or Project) Evaluation and Review
(PERT)
Computer Vision Algorithms
Artificial Intelligence Algorithms
Importance in Social Network
Rank Algorithms for Google
Etc.
75 / 91
162. Images/cinvestav-
Applications
We have several
Finding a path between nodes
Strongly Connected Components
Spanning Trees
Topological Sort - The Program (or Project) Evaluation and Review
(PERT)
Computer Vision Algorithms
Artificial Intelligence Algorithms
Importance in Social Network
Rank Algorithms for Google
Etc.
75 / 91
163. Images/cinvestav-
Outline
1 Graphs
Graphs Everywhere
History
Basic Theory
2 Graph Representation
Matrix Representation
Possible Code for This Representation
Adjacency List Representation
3 Traversing the Graph
Breadth-first search
Depth-First Search
4 Applications
Finding a path between nodes
Connected Components
Spanning Trees
Topological Sorting
76 / 91
164. Images/cinvestav-
Finding a path between nodes
We do the following
Start a breadth-first search at vertex v.
Terminate when vertex u is visited or when Q becomes empty
(whichever occurs first).
Time Complexity
O V 2 when adjacency matrix used.
O(V + E) when adjacency lists used.
77 / 91
165. Images/cinvestav-
Finding a path between nodes
We do the following
Start a breadth-first search at vertex v.
Terminate when vertex u is visited or when Q becomes empty
(whichever occurs first).
Time Complexity
O V 2 when adjacency matrix used.
O(V + E) when adjacency lists used.
77 / 91
166. Images/cinvestav-
Finding a path between nodes
We do the following
Start a breadth-first search at vertex v.
Terminate when vertex u is visited or when Q becomes empty
(whichever occurs first).
Time Complexity
O V 2 when adjacency matrix used.
O(V + E) when adjacency lists used.
77 / 91
167. Images/cinvestav-
Finding a path between nodes
We do the following
Start a breadth-first search at vertex v.
Terminate when vertex u is visited or when Q becomes empty
(whichever occurs first).
Time Complexity
O V 2 when adjacency matrix used.
O(V + E) when adjacency lists used.
77 / 91
168. Images/cinvestav-
This allow to use the Algorithm for finding The Shortest
Path
Clearly
This is the unweighted version or all weights are equal!!!
We have the following function
δ (s, v)= shortest path from s to v
We claim that
Upon termination of BFS, every vertex v ∈ V reachable from s has
distance(v) = δ(s, v)
78 / 91
169. Images/cinvestav-
This allow to use the Algorithm for finding The Shortest
Path
Clearly
This is the unweighted version or all weights are equal!!!
We have the following function
δ (s, v)= shortest path from s to v
We claim that
Upon termination of BFS, every vertex v ∈ V reachable from s has
distance(v) = δ(s, v)
78 / 91
170. Images/cinvestav-
This allow to use the Algorithm for finding The Shortest
Path
Clearly
This is the unweighted version or all weights are equal!!!
We have the following function
δ (s, v)= shortest path from s to v
We claim that
Upon termination of BFS, every vertex v ∈ V reachable from s has
distance(v) = δ(s, v)
78 / 91
171. Images/cinvestav-
Outline
1 Graphs
Graphs Everywhere
History
Basic Theory
2 Graph Representation
Matrix Representation
Possible Code for This Representation
Adjacency List Representation
3 Traversing the Graph
Breadth-first search
Depth-First Search
4 Applications
Finding a path between nodes
Connected Components
Spanning Trees
Topological Sorting
79 / 91
174. Images/cinvestav-
Procedure
First
Start a breadth-first search at any as yet unvisited vertex of the graph.
Thus
Newly visited vertices (plus edges between them) define a component.
Repeat
Repeat until all vertices are visited.
81 / 91
175. Images/cinvestav-
Procedure
First
Start a breadth-first search at any as yet unvisited vertex of the graph.
Thus
Newly visited vertices (plus edges between them) define a component.
Repeat
Repeat until all vertices are visited.
81 / 91
176. Images/cinvestav-
Procedure
First
Start a breadth-first search at any as yet unvisited vertex of the graph.
Thus
Newly visited vertices (plus edges between them) define a component.
Repeat
Repeat until all vertices are visited.
81 / 91
179. Images/cinvestav-
Outline
1 Graphs
Graphs Everywhere
History
Basic Theory
2 Graph Representation
Matrix Representation
Possible Code for This Representation
Adjacency List Representation
3 Traversing the Graph
Breadth-first search
Depth-First Search
4 Applications
Finding a path between nodes
Connected Components
Spanning Trees
Topological Sorting
83 / 91
182. Images/cinvestav-
Procedure
First
Start a breadth-first search at any vertex of the graph.
Thus
If graph is connected, the n − 1 edges used to get to unvisited vertices
define a spanning tree (breadth-first spanning tree).
85 / 91
183. Images/cinvestav-
Procedure
First
Start a breadth-first search at any vertex of the graph.
Thus
If graph is connected, the n − 1 edges used to get to unvisited vertices
define a spanning tree (breadth-first spanning tree).
85 / 91
186. Images/cinvestav-
Outline
1 Graphs
Graphs Everywhere
History
Basic Theory
2 Graph Representation
Matrix Representation
Possible Code for This Representation
Adjacency List Representation
3 Traversing the Graph
Breadth-first search
Depth-First Search
4 Applications
Finding a path between nodes
Connected Components
Spanning Trees
Topological Sorting
87 / 91
187. Images/cinvestav-
Topological Sorting
Definitions
A topological sort (sometimes abbreviated topsort or toposort) or
topological ordering of a directed graph is a linear ordering of its vertices
such that for every directed edge (u, v) from vertex u to vertex y, u comes
before v in the ordering.
From Industrial Engineering
The canonical application of topological sorting (topological order) is
in scheduling a sequence of jobs or tasks based on their dependencies.
Topological sorting algorithms were first studied in the early 1960s in
the context of the PERT technique for scheduling in project
management (Jarnagin 1960).
88 / 91
188. Images/cinvestav-
Topological Sorting
Definitions
A topological sort (sometimes abbreviated topsort or toposort) or
topological ordering of a directed graph is a linear ordering of its vertices
such that for every directed edge (u, v) from vertex u to vertex y, u comes
before v in the ordering.
From Industrial Engineering
The canonical application of topological sorting (topological order) is
in scheduling a sequence of jobs or tasks based on their dependencies.
Topological sorting algorithms were first studied in the early 1960s in
the context of the PERT technique for scheduling in project
management (Jarnagin 1960).
88 / 91
189. Images/cinvestav-
Then
We have that
The jobs are represented by vertices, and there is an edge from x to y if
job x must be completed before job y can be started.
Example
When washing clothes, the washing machine must finish before we put the
clothes to dry.
Then
A topological sort gives an order in which to perform the jobs.
89 / 91
190. Images/cinvestav-
Then
We have that
The jobs are represented by vertices, and there is an edge from x to y if
job x must be completed before job y can be started.
Example
When washing clothes, the washing machine must finish before we put the
clothes to dry.
Then
A topological sort gives an order in which to perform the jobs.
89 / 91
191. Images/cinvestav-
Then
We have that
The jobs are represented by vertices, and there is an edge from x to y if
job x must be completed before job y can be started.
Example
When washing clothes, the washing machine must finish before we put the
clothes to dry.
Then
A topological sort gives an order in which to perform the jobs.
89 / 91