Professional Documents
Culture Documents
( ) /
0
1
2
Terminology:
Degree of a Vertex
Why? Since adjacent vertices each
count the adjoining edge, it will be
counted twice
0
1 2
3 4 5 6
G1
G2
3
2
3 3
1
1
1
1
directed graph
in-degree
out-degree
0
1
2
G3
in:1, out: 1
in: 1, out: 2
in: 1, out: 0
0
1 2
3
3
3
3
Examples
28
Terminology:
Path
path: sequence of
vertices v
1
,v
2
,. . .v
k
such
that consecutive vertices
v
i
and v
i+1
are adjacent.
3
3
3
3
2
a
b
c
d
e
a b
c
d
e
a b e d c b e d c
More Terminology
simple path: no repeated vertices
cycle: simple path, except that the last vertex is the same as the first
vertex
a b
c
d
e
b e c
a c d a
a b
c
d
e
Even More Terminology
subgraph: subset of vertices and edges forming a graph
connected component: maximal connected subgraph. E.g., the graph below
has 3 connected components.
connected not connected
connected graph: any two vertices are connected by some path
0 0
1 2 3
1 2 0
1 2
3
(i) (ii) (iii) (iv)
(a) Some of the subgraph of G
1
0
0
1
0
1
2
0
1
2
(i) (ii) (iii) (iv)
(b) Some of the subgraph of G
3
0
1 2
3
G1
0
1
2
G3
Subgraphs Examples
More
tree - connected graph without cycles
forest - collection of trees
tree
forest
tree
tree
tree
Directed vs. Undirected Graph
An undirected graph is one in which the pair
of vertices in a edge is unordered, (v0, v1) =
(v1,v0)
A directed graph is one in which each edge is a
directed pair of vertices, <v0, v1> != <v1,v0>
tail
head
Graph Representations
Adjacency Matrix
Adjacency Lists
Adjacency Matrix
Let G=(V,E) be a graph with n vertices.
The adjacency matrix of G is a two-dimensional
n by n array, say adj_mat
If the edge (vi, vj) is in E(G), adj_mat[i][j]=1
If there is no such edge in E(G), adj_mat[i][j]=0
The adjacency matrix for an undirected graph is
symmetric; the adjacency matrix for a digraph
need not be symmetric
Examples for Adjacency Matrix
0
1
1
1
1
0
1
1
1
1
0
1
1
1
1
0
(
(
(
(
0
1
0
1
0
0
0
1
0
(
(
(
0
1
1
0
0
0
0
0
1
0
0
1
0
0
0
0
1
0
0
1
0
0
0
0
0
1
1
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
1
0
1
0
0
0
0
0
0
1
0
1
0
0
0
0
0
0
1
0
(
(
(
(
(
(
(
(
(
(
(
G
1
G2
G4
0
1 2
3
0
1
2
1
0
2
3
4
5
6
7
symmetric
undirected: n
2
/2
directed: n
2
0
1
2
3
0
1
2
0
1
2
3
4
5
6
7
1 2 3
0 2 3
0 1 3
0
1 2
G1
1
0 2
G3
1 2
0 3
0 3
1 2
5
4
6
5 7
6
G4
0
1 2
3
0
1
2
1
0
2
3
4
5
6
7
An undirected graph with n vertices and e edges ==> n head nodes and 2e list nodes
Depth-First Search 38
DFS : Depth-First Search
DFS is another popular search strategy.
It can do certain things that BFS cannot do. We will discuss some of
these algorithms in COMP 271 (so you cannot get rid of DFS after
COMP171).
DFS idea :
Whenever we visit a vertex v from another vertex u,
we recursively visit a neighbor of v that has not been
visited before until all neighbors of v have been visited.
Then we backtrack (return) to u.
Depth-First Search 39
Algorithm
Flag all vertices as not
visited
Visit v, and mark v as
visited.
For each unvisited neighbor.
make a recursive call
RDFS(w).
Depth-First Search 40
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
F
F
F
F
F
F
F
F
F
F
Initialize visited
table (all empty F)
Initialize Pred to -1
-
-
-
-
-
-
-
-
-
-
Pred
Depth-First Search 41
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
F
F
T
F
F
F
F
F
F
F
Mark 2 as visited
-
-
-
1
-
-
-
-
-
-
-
Pred
RDFS( 2 )
recursive call RDFS(8)
visit sequence= {2}
Depth-First Search 42
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
F
F
T
F
F
F
F
F
T
F
Mark 8 as visited
-
-
-
1
-
-
-
-
-
2
-
Pred
RDFS( 2 )
RDFS(8)
recursive callRDFS(0)
Recursive
calls
visit sequence= {2, 8}
Depth-First Search 43
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
F
T
F
F
F
F
F
T
F
Mark 0 as visited
8
-
-
1
-
-
-
-
-
2
-
Pred
RDFS( 2 )
RDFS(8)
RDFS(0) -> no unvisited neighbor, return
to (backtrack) RDFS(8)
Recursive
calls
visit sequence= {2, 8, 0}
Depth-First Search 44
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
F
T
F
F
F
F
F
T
F
8
-
-
1
-
-
-
-
-
2
-
Pred
RDFS( 2 )
RDFS(8)
recursive callRDFS(9)
Recursive
calls
Backtrack to 8
visit sequence= {2, 8, 0}
Depth-First Search 45
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
F
T
F
F
F
F
F
T
T
Mark 9 as visited
8
-
-
1
-
-
-
-
-
2
8
Pred
RDFS( 2 )
RDFS(8)
RDFS(9)
recursive callRDFS(1)
Recursive
calls
visit sequence= {2, 8, 0, 9}
Depth-First Search 46
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
F
F
F
F
F
T
T
Mark 1 as visited
8
9
-
1
-
-
-
-
-
2
8
Pred
RDFS( 2 )
RDFS(8)
RDFS(9)
RDFS(1)
recursive callRDFS(3)
Recursive
calls
visit sequence= {2, 8, 0, 9,
1}
Depth-First Search 47
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
F
F
F
F
T
T
Mark 3 as visited
8
9
-
1
1
-
-
-
-
2
8
Pred
RDFS( 2 )
RDFS(8)
RDFS(9)
RDFS(1)
RDFS(3)
recursive callRDFS(4)
Recursive
calls
visit sequence= {2, 8, 0, 9, 1, 3}
Depth-First Search 48
RDFS( 2 )
RDFS(8)
RDFS(9)
RDFS(1)
RDFS(3)
RDFS(4) STOP all of 4s neighbors have been visited
backtrack (return back) to call RDFS(3)
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
F
F
F
T
T
Mark 4 as visited
8
9
-
1
1
3
-
-
-
2
8
Pred
Recursive
calls
visit sequence= {2, 8, 0, 9, 1, 3, 4}
Depth-First Search 49
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
F
F
F
T
T
8
9
-
1
1
3
-
-
-
2
8
Pred
RDFS( 2 )
RDFS(8)
RDFS(9)
RDFS(1)
RDFS(3)
recursive callRDFS(5)
Recursive
calls
Backtrack to 3
visit sequence= {2, 8, 0, 9, 1, 3, 4}
Depth-First Search 50
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
F
F
T
T
8
9
-
1
1
3
3
-
-
2
8
Pred
RDFS( 2 )
RDFS(8)
RDFS(9)
RDFS(1)
RDFS(3)
RDFS(5)
3 is visited, recursive callRDFS(6)
Recursive
calls
Mark 5 as visited
visit sequence= {2, 8, 0, 9, 1, 3, 4, 5}
Depth-First Search 51
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
T
F
T
T
8
9
-
1
1
3
3
5
-
2
8
Pred
RDFS( 2 )
RDFS(8)
RDFS(9)
RDFS(1)
RDFS(3)
RDFS(5)
RDFS(6)
recursive call RDFS(7)
Recursive
calls
Mark 6 as visited
visit sequence= {2, 8, 0, 9, 1, 3, 4, 5,
6}
Depth-First Search 52
Example
2
4
3
5
1
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
T
T
T
T
8
9
-
1
1
3
3
5
6
2
8
Pred
RDFS( 2 )
RDFS(8)
RDFS(9)
RDFS(1)
RDFS(3)
RDFS(5)
RDFS(6)
RDFS(7)
Recursive
calls
Mark 7 as visited
visit sequence= {2, 8, 0, 9, 1, 3, 4, 5,
6, 7}
7
Depth-First Search 53
Example
2
4
3
5
1
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
T
T
T
T
8
9
-
1
1
3
3
5
6
2
8
Pred
RDFS( 2 )
RDFS(8)
RDFS(9)
RDFS(1)
RDFS(3)
RDFS(5)
RDFS(6)
RDFS(7) no recursive call
Recursive
calls
visit sequence= {2, 8, 0, 9, 1, 3, 4, 5,
6, 7}
7
Depth-First Search 54
Example
2
4
3
5
1
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
T
T
T
T
8
9
-
1
1
3
3
5
6
2
8
Pred
RDFS( 2 )
RDFS(8)
RDFS(9)
RDFS(1)
RDFS(3)
RDFS(5)
RDFS(6) no recursive call
Recursive
calls
visit sequence= {2, 8, 0, 9, 1, 3, 4, 5,
6, 7}
7
Depth-First Search 55
Example
2
4
3
5
1
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
T
T
T
T
8
9
-
1
1
3
3
5
6
2
8
Pred
RDFS( 2 )
RDFS(8)
RDFS(9)
RDFS(1)
RDFS(3)
RDFS(5) no recursive call
Recursive
calls
visit sequence= {2, 8, 0, 9, 1, 3, 4, 5,
6, 7}
7
Depth-First Search 56
Example
2
4
3
5
1
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
T
T
T
T
8
9
-
1
1
3
3
5
6
2
8
Pred RDFS( 2 )
RDFS(8)
RDFS(9)
RDFS(1)
RDFS(3) no recursive call
Recursive
calls
visit sequence= {2, 8, 0, 9, 1, 3, 4, 5,
6, 7}
7
Depth-First Search 57
Example
2
4
3
5
1
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
T
T
T
T
8
9
-
1
1
3
3
5
6
2
8
Pred RDFS( 2 )
RDFS(8)
RDFS(9)
RDFS(1) no recursive call
Recursive
calls
visit sequence= {2, 8, 0, 9, 1, 3, 4, 5,
6, 7}
7
Depth-First Search 58
Example
2
4
3
5
1
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
T
T
T
T
8
9
-
1
1
3
3
5
6
2
8
Pred
RDFS( 2 )
RDFS(8)
RDFS(9) no recursive call
Recursive
calls
visit sequence= {2, 8, 0, 9, 1, 3, 4, 5,
6, 7}
7
Depth-First Search 59
Example
2
4
3
5
1
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
T
T
T
T
8
9
-
1
1
3
3
5
6
2
8
Pred
RDFS( 2 )
RDFS(8) no recursive call
Recursive
calls
visit sequence= {2, 8, 0, 9, 1, 3, 4, 5,
6, 7}
7
Depth-First Search 60
Example
2
4
3
5
1
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
T
T
T
T
8
9
-
1
1
3
3
5
6
2
8
Pred
RDFS( 2 ) no recursive call
Recursive
calls
visit sequence= {2, 8, 0, 9, 1, 3, 4, 5,
6, 7}
7
Depth-First Search 61
Recover a path
2
4
3
5
1
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
T
T
T
T
8
9
-
1
1
3
3
5
6
2
8
Pred
visit sequence= {2, 8, 0, 9, 1, 3, 4, 5,
6, 7}
7
Try some examples.
Path(0) ->
Path(6) ->
Path(7) ->
Depth-First Search 62
DFS Tree
The edges that we traverse during DFS (or the
edges that we backtrack along) form a tree. We
usually call the rooted version (rooted at the
source) the DFS tree.
63
Minimum Spanning Trees
64
Problem: Laying Telephone Wire
Central office
65
Wiring: Nave Approach
Central office
Expensive!
66
Wiring: Better Approach
Central office
Minimize the total length of wire connecting the customers
67
Minimum Spanning Tree (MST)
(see Weiss, Section 24.2.2)
it is a tree (i.e., it is acyclic)
it covers all the vertices V
contains |V| - 1 edges
the total cost associated with tree
edges is the minimum among all
possible spanning trees
not necessarily unique
A minimum spanning tree is a subgraph of an undirected weighted
graph G, such that
Spanning Tree
Definition
A spanning tree of a graph G is a tree (acyclic) that
connects all the vertices of G once
i.e. the tree spans every vertex in G
A Minimum Spanning Tree (MST) is a spanning tree on a
weighted graph that has the minimum total weight
w T w u v
u v T
( ) ( , )
,
=
e
and P[v]
NIL
d[s]
0
S
{}
Q
Extract-Min(Q)
S
{u}
for each vertex v
e
Adj[u] do
if d[v]>d[u]+w(u,v) then
d[v]