SlideShare a Scribd company logo
1 of 196
Download to read offline
Data Structures
Graphs
Andres Mendez-Vazquez
May 8, 2015
1 / 91
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
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
Images/cinvestav-
We are full of Graphs
Maps
4 / 91
Images/cinvestav-
We are full of Graphs
State Machines for Branch Prediction in CPU’s
5 / 91
Images/cinvestav-
We are full of Graphs
Social Networks
6 / 91
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
Images/cinvestav-
History
Something Notable
Graph theory started with Euler who was asked to find a nice path across
the seven Königsberg bridges
The Actual City
8 / 91
Images/cinvestav-
History
Something Notable
Graph theory started with Euler who was asked to find a nice path across
the seven Königsberg bridges
The Actual City
8 / 91
Images/cinvestav-
There is No-Solution
Using Graphs
The (Eulerian) path should cross over each of the seven bridges exactly
once
Can you find Any possible solution?
9 / 91
Images/cinvestav-
There is No-Solution
Using Graphs
The (Eulerian) path should cross over each of the seven bridges exactly
once
Can you find Any possible solution?
9 / 91
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
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
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
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
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
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
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
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
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
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
Images/cinvestav-
Properties
Incident
An 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
Images/cinvestav-
Properties
Incident
An 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
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
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
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
Images/cinvestav-
Empty
Complete
A complete graph Kn is a simple graph with all n(n−1)/2 possible edges, like
the graph below for n = 2, 3, 4, 5.
Example
17 / 91
Images/cinvestav-
Empty
Complete
A complete graph Kn is a simple graph with all n(n−1)/2 possible edges, like
the graph below for n = 2, 3, 4, 5.
Example
17 / 91
Images/cinvestav-
We need NICE representations
First One
Matrix Representation
Second One
Adjacency Representation
18 / 91
Images/cinvestav-
We need NICE representations
First One
Matrix Representation
Second One
Adjacency Representation
18 / 91
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
Images/cinvestav-
Adjacency Matrix Representation
This is the simplest one
If we number the nodes of an undirected graph:
20 / 91
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
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
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
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
Images/cinvestav-
We have then...
Definition
0/1 N × N matrix with N =Number of nodes or vertices
A(i, j) = 1 iff (i, j) is an edge
23 / 91
Images/cinvestav-
We have then...
For the previous example
1 2 3 4 5 6
1 0 0 0 1 0 0
2 0 0 0 1 0 0
3 0 0 0 1 0 0
4 1 1 1 0 1 0
5 0 0 0 1 0 1
6 0 0 0 0 1 0
24 / 91
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
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
Images/cinvestav-
What about direct Graphs!!!
Similar idea
Use a 0 for no-edge
Use a 1 for directed edge
26 / 91
Images/cinvestav-
Example
We have that
3
1 2
4 5
6 7
1 2 3 4 5 6 7
1 0 1 1 1 0 0 0
2 0 0 0 1 1 0 0
3 0 0 0 0 0 1 0
4 0 0 1 0 0 1 1
5 0 0 0 1 0 0 1
6 0 0 0 0 0 0 0
7 0 0 0 0 0 1 0
27 / 91
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Images/cinvestav-
We use the sparse Representation of Matrices!!!
Example: Array of Row Chains
33 / 91
Images/cinvestav-
We use the sparse Representation of Matrices!!!
Example: Orthogonal Lists
34 / 91
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
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
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
Images/cinvestav-
Adjacency List Representation
Definition
Adjacency list for vertex i is a linear list of vertices adjacent from vertex i.
Basically
An array of N adjacency lists.
Thus
Each adjacency list is a chain.
38 / 91
Images/cinvestav-
Adjacency List Representation
Definition
Adjacency list for vertex i is a linear list of vertices adjacent from vertex i.
Basically
An array of N adjacency lists.
Thus
Each adjacency list is a chain.
38 / 91
Images/cinvestav-
Adjacency List Representation
Definition
Adjacency list for vertex i is a linear list of vertices adjacent from vertex i.
Basically
An array of N adjacency lists.
Thus
Each adjacency list is a chain.
38 / 91
Images/cinvestav-
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
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
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
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
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
Images/cinvestav-
Possible Disadvantage
When looking to see if an edge exist
There is no quicker way to determine if a given edge (u,v)
41 / 91
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Images/cinvestav-
Change the Order of Recursion
It is like a wave going from a node
1
2
3 4
5
6
49 / 91
Images/cinvestav-
Example
What do you see?
50 / 91
Images/cinvestav-
Example
What do you see?
51 / 91
Images/cinvestav-
Example
What do you see?
52 / 91
Images/cinvestav-
Example
What do you see?
53 / 91
Images/cinvestav-
Example
What do you see?
54 / 91
Images/cinvestav-
Example
What do you see?
55 / 91
Images/cinvestav-
Example
What do you see?
56 / 91
Images/cinvestav-
Example
What do you see?
57 / 91
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
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
Images/cinvestav-
Complexity
Overhead of Creation
O (V )
Then
Total complexity O (V + E)
59 / 91
Images/cinvestav-
Complexity
Overhead of Creation
O (V )
Then
Total complexity O (V + E)
59 / 91
Images/cinvestav-
Properties
Something Notable
Breadth-first search constructs a breadth-first tree, initially containing only
its root, which is the source vertex s
Thus
We say that u is the predecessor or parent of v in the breadth-first tree.
60 / 91
Images/cinvestav-
Properties
Something Notable
Breadth-first search constructs a breadth-first tree, initially containing only
its root, which is the source vertex s
Thus
We say that u is the predecessor or parent of v in the breadth-first tree.
60 / 91
Images/cinvestav-
Breadth-First Search Property
Something Notable
All vertices reachable from the start vertex (including the start vertex) are
visited.
61 / 91
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
Images/cinvestav-
Depth-first search
Given G
Pick an unvisited vertex v, remember the rest.
Recurse on vertices adjacent to v
63 / 91
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Images/cinvestav-
Example
What do we do?
66 / 91
Images/cinvestav-
Example
What do we do?
67 / 91
Images/cinvestav-
Example
What do we do?
68 / 91
Images/cinvestav-
Example
What do we do?
69 / 91
Images/cinvestav-
Example
What do we do?
70 / 91
Images/cinvestav-
Example
What do we do?
71 / 91
Images/cinvestav-
Example
What do we do?
72 / 91
Images/cinvestav-
Example
What do we do?
73 / 91
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Images/cinvestav-
Connected Components
Definition
A connected component (or just component) of an undirected graph is a
subgraph in which any two vertices are connected to each other by paths.
Example
80 / 91
Images/cinvestav-
Connected Components
Definition
A connected component (or just component) of an undirected graph is a
subgraph in which any two vertices are connected to each other by paths.
Example
80 / 91
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
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
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
Images/cinvestav-
Time
O (V 2
)
When adjacency matrix used
O(V + E)
When adjacency lists used (E is number of edges)
82 / 91
Images/cinvestav-
Time
O (V 2
)
When adjacency matrix used
O(V + E)
When adjacency lists used (E is number of edges)
82 / 91
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
Images/cinvestav-
Spanning Tree
Definition
A spanning tree of a graph G = (V , E) is a acyclic graph where for
u, v ∈ V , there is a path between them
Example
84 / 91
Images/cinvestav-
Spanning Tree
Definition
A spanning tree of a graph G = (V , E) is a acyclic graph where for
u, v ∈ V , there is a path between them
Example
84 / 91
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
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
Images/cinvestav-
Time
O (V 2
)
When adjacency matrix used
O(V + E)
When adjacency lists used (E is number of edges)
86 / 91
Images/cinvestav-
Time
O (V 2
)
When adjacency matrix used
O(V + E)
When adjacency lists used (E is number of edges)
86 / 91
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
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
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
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
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
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
Images/cinvestav-
Algorithm
Pseudo Code
TOPOLOGICAL-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 list
3 Return the linked list of vertices
90 / 91
Images/cinvestav-
Algorithm
Pseudo Code
TOPOLOGICAL-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 list
3 Return the linked list of vertices
90 / 91
Images/cinvestav-
Algorithm
Pseudo Code
TOPOLOGICAL-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 list
3 Return the linked list of vertices
90 / 91
Images/cinvestav-
Algorithm
Pseudo Code
TOPOLOGICAL-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 list
3 Return the linked list of vertices
90 / 91
Images/cinvestav-
Example
Dressing
91 / 91

More Related Content

What's hot

Computer Graphics in Java and Scala - Part 1
Computer Graphics in Java and Scala - Part 1Computer Graphics in Java and Scala - Part 1
Computer Graphics in Java and Scala - Part 1Philip Schwarz
 
Abstracting over the Monad yielded by a for comprehension and its generators
Abstracting over the Monad yielded by a for comprehension and its generatorsAbstracting over the Monad yielded by a for comprehension and its generators
Abstracting over the Monad yielded by a for comprehension and its generatorsPhilip Schwarz
 
27 Machine Learning Unsupervised Measure Properties
27 Machine Learning Unsupervised Measure Properties27 Machine Learning Unsupervised Measure Properties
27 Machine Learning Unsupervised Measure PropertiesAndres Mendez-Vazquez
 
Monad Transformers - Part 1
Monad Transformers - Part 1Monad Transformers - Part 1
Monad Transformers - Part 1Philip Schwarz
 
17. Trees and Tree Like Structures
17. Trees and Tree Like Structures17. Trees and Tree Like Structures
17. Trees and Tree Like StructuresIntro C# Book
 
17. Java data structures trees representation and traversal
17. Java data structures trees representation and traversal17. Java data structures trees representation and traversal
17. Java data structures trees representation and traversalIntro C# Book
 
Computer Graphics in Java and Scala - Part 1b
Computer Graphics in Java and Scala - Part 1bComputer Graphics in Java and Scala - Part 1b
Computer Graphics in Java and Scala - Part 1bPhilip Schwarz
 
Stack and Queue (brief)
Stack and Queue (brief)Stack and Queue (brief)
Stack and Queue (brief)Sanjay Saha
 
Review session2
Review session2Review session2
Review session2NEEDY12345
 
Scilab for real dummies j.heikell - part 2
Scilab for real dummies j.heikell - part 2Scilab for real dummies j.heikell - part 2
Scilab for real dummies j.heikell - part 2Scilab
 
هياكلبيانات
هياكلبياناتهياكلبيانات
هياكلبياناتRafal Edward
 
17. Trees and Graphs
17. Trees and Graphs17. Trees and Graphs
17. Trees and GraphsIntro C# Book
 
Big picture of category theory in scala with deep dive into contravariant and...
Big picture of category theory in scala with deep dive into contravariant and...Big picture of category theory in scala with deep dive into contravariant and...
Big picture of category theory in scala with deep dive into contravariant and...Piotr Paradziński
 

What's hot (20)

Computer Graphics in Java and Scala - Part 1
Computer Graphics in Java and Scala - Part 1Computer Graphics in Java and Scala - Part 1
Computer Graphics in Java and Scala - Part 1
 
Chapter 4 ds
Chapter 4 dsChapter 4 ds
Chapter 4 ds
 
Chapter 5 ds
Chapter 5 dsChapter 5 ds
Chapter 5 ds
 
Chapter 9 ds
Chapter 9 dsChapter 9 ds
Chapter 9 ds
 
Abstracting over the Monad yielded by a for comprehension and its generators
Abstracting over the Monad yielded by a for comprehension and its generatorsAbstracting over the Monad yielded by a for comprehension and its generators
Abstracting over the Monad yielded by a for comprehension and its generators
 
27 Machine Learning Unsupervised Measure Properties
27 Machine Learning Unsupervised Measure Properties27 Machine Learning Unsupervised Measure Properties
27 Machine Learning Unsupervised Measure Properties
 
Monad Transformers - Part 1
Monad Transformers - Part 1Monad Transformers - Part 1
Monad Transformers - Part 1
 
17. Trees and Tree Like Structures
17. Trees and Tree Like Structures17. Trees and Tree Like Structures
17. Trees and Tree Like Structures
 
17. Java data structures trees representation and traversal
17. Java data structures trees representation and traversal17. Java data structures trees representation and traversal
17. Java data structures trees representation and traversal
 
Chapter 7 ds
Chapter 7 dsChapter 7 ds
Chapter 7 ds
 
Chapter 5
Chapter 5Chapter 5
Chapter 5
 
Computer Graphics in Java and Scala - Part 1b
Computer Graphics in Java and Scala - Part 1bComputer Graphics in Java and Scala - Part 1b
Computer Graphics in Java and Scala - Part 1b
 
Stack and Queue (brief)
Stack and Queue (brief)Stack and Queue (brief)
Stack and Queue (brief)
 
Review session2
Review session2Review session2
Review session2
 
Scilab for real dummies j.heikell - part 2
Scilab for real dummies j.heikell - part 2Scilab for real dummies j.heikell - part 2
Scilab for real dummies j.heikell - part 2
 
هياكلبيانات
هياكلبياناتهياكلبيانات
هياكلبيانات
 
05 queues
05 queues05 queues
05 queues
 
17. Trees and Graphs
17. Trees and Graphs17. Trees and Graphs
17. Trees and Graphs
 
Functor Composition
Functor CompositionFunctor Composition
Functor Composition
 
Big picture of category theory in scala with deep dive into contravariant and...
Big picture of category theory in scala with deep dive into contravariant and...Big picture of category theory in scala with deep dive into contravariant and...
Big picture of category theory in scala with deep dive into contravariant and...
 

Similar to Preparation Data Structures 11 graphs

Machine Learning Algorithms (Part 1)
Machine Learning Algorithms (Part 1)Machine Learning Algorithms (Part 1)
Machine Learning Algorithms (Part 1)Zihui Li
 
23 Machine Learning Feature Generation
23 Machine Learning Feature Generation23 Machine Learning Feature Generation
23 Machine Learning Feature GenerationAndres Mendez-Vazquez
 
Graph Analytics - From the Whiteboard to Your Toolbox - Sam Lerma
Graph Analytics - From the Whiteboard to Your Toolbox - Sam LermaGraph Analytics - From the Whiteboard to Your Toolbox - Sam Lerma
Graph Analytics - From the Whiteboard to Your Toolbox - Sam LermaPyData
 
Graph Analyses with Python and NetworkX
Graph Analyses with Python and NetworkXGraph Analyses with Python and NetworkX
Graph Analyses with Python and NetworkXBenjamin Bengfort
 
Graph mining 2: Statistical approaches for graph mining
Graph mining 2: Statistical approaches for graph miningGraph mining 2: Statistical approaches for graph mining
Graph mining 2: Statistical approaches for graph miningtuxette
 
Lecture 2.3.1 Graph.pptx
Lecture 2.3.1 Graph.pptxLecture 2.3.1 Graph.pptx
Lecture 2.3.1 Graph.pptxking779879
 
Social network-analysis-in-python
Social network-analysis-in-pythonSocial network-analysis-in-python
Social network-analysis-in-pythonJoe OntheRocks
 
Minimizing cost in distributed multiquery processing applications
Minimizing cost in distributed multiquery processing applicationsMinimizing cost in distributed multiquery processing applications
Minimizing cost in distributed multiquery processing applicationsLuis Galárraga
 
CS 354 More Graphics Pipeline
CS 354 More Graphics PipelineCS 354 More Graphics Pipeline
CS 354 More Graphics PipelineMark Kilgard
 
Data Clustering with R
Data Clustering with RData Clustering with R
Data Clustering with RYanchang Zhao
 
Graphs and eularian circuit & path with c++ program
Graphs and eularian circuit & path with c++ programGraphs and eularian circuit & path with c++ program
Graphs and eularian circuit & path with c++ programMuhammad Danish Badar
 
Technical aptitude questions_e_book1
Technical aptitude questions_e_book1Technical aptitude questions_e_book1
Technical aptitude questions_e_book1Sateesh Allu
 
Applied parallel coordinates for logs and network traffic attack analysis
Applied parallel coordinates for logs and network traffic attack analysisApplied parallel coordinates for logs and network traffic attack analysis
Applied parallel coordinates for logs and network traffic attack analysisUltraUploader
 
An analysis between exact and approximate algorithms for the k-center proble...
An analysis between exact and approximate algorithms for the  k-center proble...An analysis between exact and approximate algorithms for the  k-center proble...
An analysis between exact and approximate algorithms for the k-center proble...IJECEIAES
 
An Optimal Approach For Knowledge Protection In Structured Frequent Patterns
An Optimal Approach For Knowledge Protection In Structured Frequent PatternsAn Optimal Approach For Knowledge Protection In Structured Frequent Patterns
An Optimal Approach For Knowledge Protection In Structured Frequent PatternsWaqas Tariq
 

Similar to Preparation Data Structures 11 graphs (20)

18 Basic Graph Algorithms
18 Basic Graph Algorithms18 Basic Graph Algorithms
18 Basic Graph Algorithms
 
Machine Learning Algorithms (Part 1)
Machine Learning Algorithms (Part 1)Machine Learning Algorithms (Part 1)
Machine Learning Algorithms (Part 1)
 
Dijkstra
DijkstraDijkstra
Dijkstra
 
d
dd
d
 
23 Machine Learning Feature Generation
23 Machine Learning Feature Generation23 Machine Learning Feature Generation
23 Machine Learning Feature Generation
 
Graph Analytics - From the Whiteboard to Your Toolbox - Sam Lerma
Graph Analytics - From the Whiteboard to Your Toolbox - Sam LermaGraph Analytics - From the Whiteboard to Your Toolbox - Sam Lerma
Graph Analytics - From the Whiteboard to Your Toolbox - Sam Lerma
 
Graph Analyses with Python and NetworkX
Graph Analyses with Python and NetworkXGraph Analyses with Python and NetworkX
Graph Analyses with Python and NetworkX
 
Graph mining 2: Statistical approaches for graph mining
Graph mining 2: Statistical approaches for graph miningGraph mining 2: Statistical approaches for graph mining
Graph mining 2: Statistical approaches for graph mining
 
Lecture 2.3.1 Graph.pptx
Lecture 2.3.1 Graph.pptxLecture 2.3.1 Graph.pptx
Lecture 2.3.1 Graph.pptx
 
Social network-analysis-in-python
Social network-analysis-in-pythonSocial network-analysis-in-python
Social network-analysis-in-python
 
Graphs
GraphsGraphs
Graphs
 
Twopi.1
Twopi.1Twopi.1
Twopi.1
 
Minimizing cost in distributed multiquery processing applications
Minimizing cost in distributed multiquery processing applicationsMinimizing cost in distributed multiquery processing applications
Minimizing cost in distributed multiquery processing applications
 
CS 354 More Graphics Pipeline
CS 354 More Graphics PipelineCS 354 More Graphics Pipeline
CS 354 More Graphics Pipeline
 
Data Clustering with R
Data Clustering with RData Clustering with R
Data Clustering with R
 
Graphs and eularian circuit & path with c++ program
Graphs and eularian circuit & path with c++ programGraphs and eularian circuit & path with c++ program
Graphs and eularian circuit & path with c++ program
 
Technical aptitude questions_e_book1
Technical aptitude questions_e_book1Technical aptitude questions_e_book1
Technical aptitude questions_e_book1
 
Applied parallel coordinates for logs and network traffic attack analysis
Applied parallel coordinates for logs and network traffic attack analysisApplied parallel coordinates for logs and network traffic attack analysis
Applied parallel coordinates for logs and network traffic attack analysis
 
An analysis between exact and approximate algorithms for the k-center proble...
An analysis between exact and approximate algorithms for the  k-center proble...An analysis between exact and approximate algorithms for the  k-center proble...
An analysis between exact and approximate algorithms for the k-center proble...
 
An Optimal Approach For Knowledge Protection In Structured Frequent Patterns
An Optimal Approach For Knowledge Protection In Structured Frequent PatternsAn Optimal Approach For Knowledge Protection In Structured Frequent Patterns
An Optimal Approach For Knowledge Protection In Structured Frequent Patterns
 

More from Andres Mendez-Vazquez

01.04 orthonormal basis_eigen_vectors
01.04 orthonormal basis_eigen_vectors01.04 orthonormal basis_eigen_vectors
01.04 orthonormal basis_eigen_vectorsAndres Mendez-Vazquez
 
01.03 squared matrices_and_other_issues
01.03 squared matrices_and_other_issues01.03 squared matrices_and_other_issues
01.03 squared matrices_and_other_issuesAndres Mendez-Vazquez
 
05 backpropagation automatic_differentiation
05 backpropagation automatic_differentiation05 backpropagation automatic_differentiation
05 backpropagation automatic_differentiationAndres Mendez-Vazquez
 
01 Introduction to Neural Networks and Deep Learning
01 Introduction to Neural Networks and Deep Learning01 Introduction to Neural Networks and Deep Learning
01 Introduction to Neural Networks and Deep LearningAndres Mendez-Vazquez
 
25 introduction reinforcement_learning
25 introduction reinforcement_learning25 introduction reinforcement_learning
25 introduction reinforcement_learningAndres Mendez-Vazquez
 
Neural Networks and Deep Learning Syllabus
Neural Networks and Deep Learning SyllabusNeural Networks and Deep Learning Syllabus
Neural Networks and Deep Learning SyllabusAndres Mendez-Vazquez
 
Introduction to artificial_intelligence_syllabus
Introduction to artificial_intelligence_syllabusIntroduction to artificial_intelligence_syllabus
Introduction to artificial_intelligence_syllabusAndres Mendez-Vazquez
 
Ideas about a Bachelor in Machine Learning/Data Sciences
Ideas about a Bachelor in Machine Learning/Data SciencesIdeas about a Bachelor in Machine Learning/Data Sciences
Ideas about a Bachelor in Machine Learning/Data SciencesAndres Mendez-Vazquez
 
20 k-means, k-center, k-meoids and variations
20 k-means, k-center, k-meoids and variations20 k-means, k-center, k-meoids and variations
20 k-means, k-center, k-meoids and variationsAndres Mendez-Vazquez
 

More from Andres Mendez-Vazquez (20)

2.03 bayesian estimation
2.03 bayesian estimation2.03 bayesian estimation
2.03 bayesian estimation
 
05 linear transformations
05 linear transformations05 linear transformations
05 linear transformations
 
01.04 orthonormal basis_eigen_vectors
01.04 orthonormal basis_eigen_vectors01.04 orthonormal basis_eigen_vectors
01.04 orthonormal basis_eigen_vectors
 
01.03 squared matrices_and_other_issues
01.03 squared matrices_and_other_issues01.03 squared matrices_and_other_issues
01.03 squared matrices_and_other_issues
 
01.02 linear equations
01.02 linear equations01.02 linear equations
01.02 linear equations
 
01.01 vector spaces
01.01 vector spaces01.01 vector spaces
01.01 vector spaces
 
06 recurrent neural_networks
06 recurrent neural_networks06 recurrent neural_networks
06 recurrent neural_networks
 
05 backpropagation automatic_differentiation
05 backpropagation automatic_differentiation05 backpropagation automatic_differentiation
05 backpropagation automatic_differentiation
 
Zetta global
Zetta globalZetta global
Zetta global
 
01 Introduction to Neural Networks and Deep Learning
01 Introduction to Neural Networks and Deep Learning01 Introduction to Neural Networks and Deep Learning
01 Introduction to Neural Networks and Deep Learning
 
25 introduction reinforcement_learning
25 introduction reinforcement_learning25 introduction reinforcement_learning
25 introduction reinforcement_learning
 
Neural Networks and Deep Learning Syllabus
Neural Networks and Deep Learning SyllabusNeural Networks and Deep Learning Syllabus
Neural Networks and Deep Learning Syllabus
 
Introduction to artificial_intelligence_syllabus
Introduction to artificial_intelligence_syllabusIntroduction to artificial_intelligence_syllabus
Introduction to artificial_intelligence_syllabus
 
Ideas 09 22_2018
Ideas 09 22_2018Ideas 09 22_2018
Ideas 09 22_2018
 
Ideas about a Bachelor in Machine Learning/Data Sciences
Ideas about a Bachelor in Machine Learning/Data SciencesIdeas about a Bachelor in Machine Learning/Data Sciences
Ideas about a Bachelor in Machine Learning/Data Sciences
 
Analysis of Algorithms Syllabus
Analysis of Algorithms  SyllabusAnalysis of Algorithms  Syllabus
Analysis of Algorithms Syllabus
 
20 k-means, k-center, k-meoids and variations
20 k-means, k-center, k-meoids and variations20 k-means, k-center, k-meoids and variations
20 k-means, k-center, k-meoids and variations
 
18.1 combining models
18.1 combining models18.1 combining models
18.1 combining models
 
17 vapnik chervonenkis dimension
17 vapnik chervonenkis dimension17 vapnik chervonenkis dimension
17 vapnik chervonenkis dimension
 
A basic introduction to learning
A basic introduction to learningA basic introduction to learning
A basic introduction to learning
 

Recently uploaded

The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...RKavithamani
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 

Recently uploaded (20)

The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 

Preparation Data Structures 11 graphs

  • 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
  • 4. Images/cinvestav- We are full of Graphs Maps 4 / 91
  • 5. Images/cinvestav- We are full of Graphs State Machines for Branch Prediction in CPU’s 5 / 91
  • 6. Images/cinvestav- We are full of Graphs Social Networks 6 / 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
  • 8. Images/cinvestav- History Something Notable Graph theory started with Euler who was asked to find a nice path across the seven Königsberg bridges The Actual City 8 / 91
  • 9. Images/cinvestav- History Something Notable Graph theory started with Euler who was asked to find a nice path across the seven Königsberg bridges The Actual City 8 / 91
  • 10. Images/cinvestav- There is No-Solution Using Graphs The (Eulerian) path should cross over each of the seven bridges exactly once Can you find Any possible solution? 9 / 91
  • 11. Images/cinvestav- There is No-Solution Using Graphs The (Eulerian) path should cross over each of the seven bridges exactly once Can you find Any possible solution? 9 / 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
  • 22. Images/cinvestav- Properties Incident An 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
  • 23. Images/cinvestav- Properties Incident An 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
  • 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
  • 27. Images/cinvestav- Empty Complete A complete graph Kn is a simple graph with all n(n−1)/2 possible edges, like the graph below for n = 2, 3, 4, 5. Example 17 / 91
  • 28. Images/cinvestav- Empty Complete A complete graph Kn is a simple graph with all n(n−1)/2 possible edges, like the graph below for n = 2, 3, 4, 5. Example 17 / 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
  • 32. Images/cinvestav- Adjacency Matrix Representation This is the simplest one If we number the nodes of an undirected graph: 20 / 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
  • 37. Images/cinvestav- We have then... Definition 0/1 N × N matrix with N =Number of nodes or vertices A(i, j) = 1 iff (i, j) is an edge 23 / 91
  • 38. Images/cinvestav- We have then... For the previous example 1 2 3 4 5 6 1 0 0 0 1 0 0 2 0 0 0 1 0 0 3 0 0 0 1 0 0 4 1 1 1 0 1 0 5 0 0 0 1 0 1 6 0 0 0 0 1 0 24 / 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
  • 41. Images/cinvestav- What about direct Graphs!!! Similar idea Use a 0 for no-edge Use a 1 for directed edge 26 / 91
  • 42. Images/cinvestav- Example We have that 3 1 2 4 5 6 7 1 2 3 4 5 6 7 1 0 1 1 1 0 0 0 2 0 0 0 1 1 0 0 3 0 0 0 0 0 1 0 4 0 0 1 0 0 1 1 5 0 0 0 1 0 0 1 6 0 0 0 0 0 0 0 7 0 0 0 0 0 1 0 27 / 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
  • 58. Images/cinvestav- We use the sparse Representation of Matrices!!! Example: Array of Row Chains 33 / 91
  • 59. Images/cinvestav- We use the sparse Representation of Matrices!!! Example: Orthogonal Lists 34 / 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
  • 63. Images/cinvestav- Adjacency List Representation Definition Adjacency list for vertex i is a linear list of vertices adjacent from vertex i. Basically An array of N adjacency lists. Thus Each adjacency list is a chain. 38 / 91
  • 64. Images/cinvestav- Adjacency List Representation Definition Adjacency list for vertex i is a linear list of vertices adjacent from vertex i. Basically An array of N adjacency lists. Thus Each adjacency list is a chain. 38 / 91
  • 65. Images/cinvestav- Adjacency List Representation Definition Adjacency list for vertex i is a linear list of vertices adjacent from vertex i. Basically An array of N adjacency lists. Thus Each adjacency list is a chain. 38 / 91
  • 66. Images/cinvestav- 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
  • 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
  • 71. Images/cinvestav- Possible Disadvantage When looking to see if an edge exist There is no quicker way to determine if a given edge (u,v) 41 / 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
  • 106. Images/cinvestav- Change the Order of Recursion It is like a wave going from a node 1 2 3 4 5 6 49 / 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
  • 117. Images/cinvestav- Complexity Overhead of Creation O (V ) Then Total complexity O (V + E) 59 / 91
  • 118. Images/cinvestav- Complexity Overhead of Creation O (V ) Then Total complexity O (V + E) 59 / 91
  • 119. Images/cinvestav- Properties Something Notable Breadth-first search constructs a breadth-first tree, initially containing only its root, which is the source vertex s Thus We say that u is the predecessor or parent of v in the breadth-first tree. 60 / 91
  • 120. Images/cinvestav- Properties Something Notable Breadth-first search constructs a breadth-first tree, initially containing only its root, which is the source vertex s Thus We say that u is the predecessor or parent of v in the breadth-first tree. 60 / 91
  • 121. Images/cinvestav- Breadth-First Search Property Something Notable All vertices reachable from the start vertex (including the start vertex) are visited. 61 / 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
  • 123. Images/cinvestav- Depth-first search Given G Pick an unvisited vertex v, remember the rest. Recurse on vertices adjacent to v 63 / 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
  • 172. Images/cinvestav- Connected Components Definition A connected component (or just component) of an undirected graph is a subgraph in which any two vertices are connected to each other by paths. Example 80 / 91
  • 173. Images/cinvestav- Connected Components Definition A connected component (or just component) of an undirected graph is a subgraph in which any two vertices are connected to each other by paths. Example 80 / 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
  • 177. Images/cinvestav- Time O (V 2 ) When adjacency matrix used O(V + E) When adjacency lists used (E is number of edges) 82 / 91
  • 178. Images/cinvestav- Time O (V 2 ) When adjacency matrix used O(V + E) When adjacency lists used (E is number of edges) 82 / 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
  • 180. Images/cinvestav- Spanning Tree Definition A spanning tree of a graph G = (V , E) is a acyclic graph where for u, v ∈ V , there is a path between them Example 84 / 91
  • 181. Images/cinvestav- Spanning Tree Definition A spanning tree of a graph G = (V , E) is a acyclic graph where for u, v ∈ V , there is a path between them Example 84 / 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
  • 184. Images/cinvestav- Time O (V 2 ) When adjacency matrix used O(V + E) When adjacency lists used (E is number of edges) 86 / 91
  • 185. Images/cinvestav- Time O (V 2 ) When adjacency matrix used O(V + E) When adjacency lists used (E is number of edges) 86 / 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
  • 192. Images/cinvestav- Algorithm Pseudo Code TOPOLOGICAL-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 list 3 Return the linked list of vertices 90 / 91
  • 193. Images/cinvestav- Algorithm Pseudo Code TOPOLOGICAL-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 list 3 Return the linked list of vertices 90 / 91
  • 194. Images/cinvestav- Algorithm Pseudo Code TOPOLOGICAL-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 list 3 Return the linked list of vertices 90 / 91
  • 195. Images/cinvestav- Algorithm Pseudo Code TOPOLOGICAL-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 list 3 Return the linked list of vertices 90 / 91