Graphs
Graphs
CH. 6
GRAPHS
3
6.1.2 Graph Definition
A graph, G, consists of two sets, V and E.
◦ G = (V, E)
◦ V: a set of vertices.
◦ E: a set of pairs of vertices called edges.
Undirected graph (simply graph)
◦ (𝑢, 𝑣) and (𝑣, 𝑢) represent the same edge.
Directed graph (digraph)
◦ < 𝑢, 𝑣 > ≠ < 𝑣, 𝑢 >
◦ < 𝑢, 𝑣 > ⇒ 𝑢 is head and 𝑣 is tail of edge.
𝑢 𝑣
4
Graph Examples
0 0 0
1 2 1 2
1
3
3 4 5 6 2
5
Restrictions
Self edges and self loops are not permitted!
◦ Edges of the form (𝑣, 𝑣) and <𝑣, 𝑣> are not legal.
A graph may not have multiple occurrences of the
same edge (multigraph).
0 0
1 3 1 3
2 2
6
Graph, Vertex and Edge
For a graph with 𝑛 vertices, the maximum #
of edges is:
◦ 𝑛(𝑛 − 1)/2 for undirected graph
◦ 𝑛(𝑛 − 1) for directed graph
Vertices 𝑢 and 𝑣 are adjacent if 𝑢, 𝑣 ∈ 𝐸
and edge (𝑢, 𝑣) is incident on vertices 𝑢
and 𝑣.
For a directed edge < 𝑢, 𝑣 >, we say 𝑢 is
adjacent to 𝑣 and 𝑣 is adjacent from 𝑢,
and edge (𝑢, 𝑣) is incident on vertices 𝑢
and 𝑣.
7
Complete Graph
Complete undirected graph Complete directed graph
• Graph with 𝑛 vertices has Graph with 𝑛 vertices
exactly 𝑛(𝑛 − 1)/2 edges. has exactly 𝑛(𝑛 − 1)
edges.
0
0
1 2
1 2
3
8
Subgraph
𝐺’ is a subgraph of 𝐺 such that
𝑉(𝐺’) 𝑉(𝐺) and 𝐸(𝐺’) 𝐸(𝐺).
0 0
1 2
1 2 1 2
3
3 3
9
Path and Simple Path
Path:
◦ A path from 𝒖 to 𝒗 represents a sequence of
vertices 𝒖, 𝒊𝟏, 𝒊𝟐, … , 𝒊𝒌, 𝒗 such that
(𝒖, 𝒊𝟏), (𝒊𝟏, 𝒊𝟐), … , (𝒊𝒌, 𝒗) are edges in graph.
Simple path:
◦ A simple path is a path in which all vertices
except possibly the first and the last are distinct.
10
Cycle
A cycle is a simple path which the first and
the last vertices are the same.
11
Connected Graph
Undirected graph G is said to be
connected iff for every pair of distinct
vertices 𝒖 and 𝒗, there is a path from 𝒖 to
𝒗 in G.
0 0 4
1 2 1 2 5 7
3 3 6
12
Connected Component & Tree
A connected component, H, of an
undirected graph is a maximal connected
subgraph.
0 4
1 2 5 7
3 H1 6 H2
13
Strongly Connected
Directed graph G is said to be strongly
connected iff for every pair of distinct
vertices 𝒖 and 𝒗, there is a directed path
from 𝒖 to 𝒗 and also from 𝒗 to 𝒖 in G.
14
Strongly Connected Component
A strongly connected component is a
maximal subgraph that is strongly
connected.
0
0
1 2
1
2 H1 H2
15
Degree of a Vertex
Degree of a vertex 𝑣:
◦ The # of edges incident to 𝑣.
In a directed graph:
◦ In-degree of 𝑣
The # of edges for which 𝑣 is the tail (incoming edges).
◦ Out-degree of 𝑣
The # of edges for which 𝑣 is the head (outgoing edges).
◦ Degree of 𝒗 = in-degree + out-degree
2
1 3
16
6.1.3 GRAPH
REPRESENTATION
17
Adjacency Matrix
A two dimensional array with the property that
𝑎[𝑖][𝑗] = 1 iff the edge (𝑖, 𝑗) or < 𝑖, 𝑗 > is in 𝐸(𝐺).
0 0
1 2 1
3 2
18
Adjacency Lists
Undirected graph: Use a chain to represent
each vertex and its adjacent vertices.
adjLists
0 [0] 3 1 2 0
[1] 2 3 0 0
1 2
[2] 1 3 0 0
3 [3] 0 1 2 0
19
Adjacency Lists
Directed graph: Use a chain to represent
each vertex and its adjacent vertices.
◦ Length of list = out-degree of 𝑣
0
adjLists
[0] 1 0
1
[1] 2 0 0
2 [2] NULL
20
Inverse Adjacency Lists
Directed graph: Use a chain to represent each
vertex and its adjacent from vertices
◦ Length of list = in-degree of 𝑣
0
adjLists
[0] 1 0
1
[1] 0 0
2 [2] 1 0
21
Sequential Representation
Example: 𝑛 = 4, 𝑒 = 4
Int nodes[𝑛 + 2𝑒 + 1] => nodes[13]
1 2
3
nodes edges
0 1 2 3 4 5 6 7 8 9 10 11 12
5 7 9 10 13 1 3 0 3 3 0 1 2
To-node
0 Header
0 1 2
nodes
0 0 1 0 0
1
1 1 0 0 1 2 0 0
2 2 0
From-node
24
How Many Kinds of Graphs ?
2 types:
◦ Directed,
◦ Undirected
2 edge types:
◦ Weighted,
◦ Unweighted
4 representations:
◦ Adjacency matrix,
◦ Adjacency lists,
◦ Sequential lists,
◦ Adjacency multilists
25
ADT: Graph
class Graph
{// object: A nonempty set of vertices and a set of undirected edges.
public:
virtual ~Graph() {} // virtual destructor
bool IsEmpty() const{return n == 0}; // return true iff graph has no
vertices
int NumberOfVertices() const{return n}; // return the # of vertices
int NumberOfEdges() const{return e}; // return the # of edges
virtual int Degree(int u) const = 0; // return the degree of a vertex
virtual bool ExistsEdge(int u, int v) const = 0; // check the existence
of edge
virtual void InsertVertex(int v) = 0; // insert a vertex v
virtual void InsertEdge(int u, int v) = 0; // insert an edge (u, v)
virtual void DeleteVertex(int v) = 0; // delete a vertex v
virtual void DeleteEdge(int u, int v) = 0; // delete an edge (u, v)
// More graph operations…
protected:
int n; // number of vertices
int e; // number of edges
};
26
Implementation Notes
To accommodate various graph types, we
make the following assumptions:
Data type of edge weight is double (or
represented as a template parameter).
We define operations which are
independent of specific graph
representation in the Graph.
We assume the iterator is used to visit
adjacent vertices.
27
Example: LinkedGraph
void Graph::foo(void){
// use iterator to visit adjacent vertices of v
for (each vertex w adjacent to v)…
}
28
6.2
Elementary
Graph
Operations
30
6.2.1 Depth-First Search (DFS)
Starting from a vertex 𝑣
◦ Visit the vertex 𝑣 ⇒ DFS(𝑣).
◦ For each vertex 𝑤 adjacent to 𝑣, if 𝑤 is not visited
yet, then visit 𝑤 ⇒ DFS(𝑤).
◦ If a vertex 𝑢 is reached such that all its adjacent
vertices have been visited, we go back to the last
visited vertex.
The search terminates when no unvisited
vertex can be reached from any of the
visited vertices.
31
Depth-First Search (DFS)
1 2
0 1 3 7
3 4 5 6 4 5 2 6
32
Recursive DFS
void Graph::DFS(void){
visited = new bool[n]; // this is a data member of Graph
fill(visited, visited+n, false);
DFS(0); // start search at vertex 0
delete [] visited;
}
33
Non-Recursive DFS
void Graph::DFS(int v){
visited = new bool[n]; // this is a data member of Graph
fill(visited, visited+n, false);
Stack<int> s; // declare and init a stack
s.Push(v);
while(!s.IsEmpty()){
v = s.Top(); s.Pop();
if(!visited[v]){
output(v);
visited[v]=true;
for(each vertex w adjacent to v)
if(!visited[w]) s.Push(w);
}
}
}
34
DFS Complexity
Adjacency matrix
◦ Time to determine all adjacent vertices: 𝑂(𝑛)
◦ At most 𝑛 vertices are visited:
𝑂(𝑛 ∙ 𝑛) = 𝑂(𝑛2)
Adjacency lists
◦ There are 𝑛 + 2𝑒 chain nodes
◦ Each node in the adjacency list is examined at
most once. Time complexity = 𝑂(𝑒)
35
6.2.2 Breadth-First Search (BFS)
Starting from a vertex 𝑣
◦ Visit the vertex 𝑣 .
◦ Visit all unvisited vertices adjacent to 𝑣.
◦ Unvisited vertices adjacent to these newly visited
vertices are then visited and so on…
36
Breadth-First Search (BFS)
1 2
0 1 2 3
3 4 5 6 4 5 6 7
37
BFS: Implementation
void Graph::BFS(int v){
visited = new bool[n]; // this is a data member of Graph
fill(visited, visited+n, false);
Queue<int> q; // declare and init a queue
q.Push(v);
visited[v]=true;
while(!q.IsEmpty()){
v = q.Front(); q.Pop();
output(v);
for(each vertex w adjacent to v){
if(!visited[w]){
q.Push(w);
visited[w]=true;
}
}
}
delete [] visited; Time complexity is the same as DFS
}
38
6.2.3 Connected Components
How to determine whether a graph is
connected or not?
◦ Call DFS or BFS once and check if there is any
unvisited vertices, if Yes, then the graph is not
connected.
How to identify connected components
◦ Make a repeated calls to DFS or BFS.
◦ Each call will output a connected component.
◦ Start next call at an unvisited vertex.
39
6.2.4 Spanning Trees
Definition: Any tree consists of solely of
edges in 𝐸(𝐺) and including all vertices of
𝑉(𝐺).
Number of tree edges is 𝒏 − 𝟏.
Add a non-tree edge will create a cycle.
0 0 0 0
1 2 1 2 1 2 1 2
3 3 3 3
3 4 5 6 3 4 5 6
7 7
41
BFS Spanning Tree
Tree edges are those edges met during the
traversal.
0 1 2 3
0 0
4 5 6 7
1 2 1 2
BFS
3 4 5 6 3 4 5 6
7 7
42
6.3
Minimum Cost
Spanning Trees
0 0
28
making the locally 10
1
10
1
optimal choice at 14 16 16
each stage with the 5 6 2 5 6 2
24 24
hope of finding a 25 18 12 25 18 12
global optimum 4 3 4 3
22
44
6.3.1 Kruskal’s Algorithm
Idea: Add edges with minimum edge weight
to tree one at a time.
1. Find an edge with minimum cost.
2. If it creates a cycle, discard the edge.
3. Repeat step 1 and 2 until we find 𝑛 − 1
edges.
Martin Kruskal
1925~2006
45
Example for Kruskal’s Alg.
Refer to the textbook for detailed steps!
0 0
28
10 1
10 1
14 16 14 16
5 6 2 5 6 2
24
25 18 12 25 12
4 3 4 3
22 22
Connected graph Spanning tree with cost 99
46
Kruskal’s Algo. Implementation
Use min heap to store edge cost.
Use set representation to group all vertices
in the same connected component into a set.
◦ For an edge (𝑣, 𝑤) to be added, if vertices are in
the same set, discard the edge, else merge two
sets.
Kruskal’s algorithm
1. T = ψ
2. While((T contains less than n-1 edges)&&(E is not empty)){
3. choose an edge (v,w) from E of lowest cost;
4. delete (v,w) from E
5. if((v,w) does not create a cycle) add (v,w) to T;
6. else discard (v,w)
7. }
8. If(T contains less than n-1 edges)
9. cout << “there is no spanning tree!” <<endl;
47
Time Complexity
Min heap: 𝑂(log 𝑒)
Set: 𝑂(𝑎(𝑒))
At most execute 𝑒 rounds:
◦ 𝑒 ∙ (log 𝑒 + 𝑎(𝑒)) = 𝑂(𝑒 log 𝑒)
48
6.3.2 Prim’s algorithm
Idea: Add edges with minimum edge weight
to tree one at a time. At all times during the
algorithm, the set of selected edges form
a tree.
1. Start with a tree 𝑇 contains a single
arbitrary vertex.
2. Among all edges, add a least cost edge
(𝑢, 𝑣) to 𝑇 such that 𝑇 ∪ (𝑢, 𝑣) is still a tree.
3. Repeat step 2 until 𝑇 contains 𝑛 − 1 edges.
54
Example for Prim’s Alg.
0
28
10 1 near-to-tree 0 1 2 3 4 5 6
14 16
V(T)={0} * 28 ∞ ∞ ∞ 10 ∞
5 6 2
24 V(T)={0,5}
25 18 12 * 28 ∞ ∞ 25 * ∞
4 3
22 V(T)={0,5,4} * 28 ∞ 22 * * 24
Connected graph
V(T)={0,5,4,3} * 28 12 * * * 18
0
10
V(T)={0,5,4,3,2} * 16 * * * * 18
1
14 16
V(T)={0,5,4,3,2,1} * * * * * * 14
5 6 2
25 12
V(T)={0,5,4,3,2,1,6}
4 3
22
Spanning tree with cost 99 55
Prim’s Algorithm
Step 3: use a near-to-tree data structure
◦ Create an array to record the nearest distance of
vertices to 𝑇.
◦ Only vertices not in 𝑉(𝑇) and adjacent to 𝑇 are
recorded.
Prim’s algorithm
1. V(T) = {0} // start with vertex 0
2. for(T=ψ ; T contains less than n-1 edges; add (u,v) to T){
3. Let (u,v) be a least cost edge such that u∈V(T) and v∉V(T);
4. if(there is no such edge) break;
5. add v to V(T);
6. }
7. If(T contains fewer than n-1 edges)
8. cout << “there is no spanning tree!” <<endl;
56
Time Complexity
Near-to-tree
◦ Step 3: 𝑂(𝑛)
At most execute 𝑛 rounds: 𝑂(𝑛2)
57
6.3.3 Sollin’s Algorithm
Idea: Select several edges at each stage.
1. Start with a forest that has n spanning trees
(each has one vertex).
2. Select one minimum cost edge for each tree.
This edge has exactly one vertex in the tree.
3. Delete multiple copies of selected edges and
if two edges with the same cost connecting
two trees, keep only one of them.
4. Add these selected edges to the forest.
5. Repeat until we obtain only one tree.
58
Example for Sollin’s Alg.
Refer to the textbook for detailed steps!
0 0
28
10 1
10 1
14 16 14 16
5 6 2 5 6 2
24
25 18 12 25 12
4 3 4 3
22 22
Connected graph Spanning tree with cost 99
59
6.4
Shortest Paths
and Transitive
Closure
The algorithm
1. Let S={v}, all entries in dist = ∞
2. For each vertex w not in S, update dist
𝑑𝑖𝑠[𝑤]=𝑚𝑖𝑛(𝑑𝑖𝑠𝑡[𝑢]+𝑙𝑒𝑛𝑔𝑡ℎ(⟨𝑢,𝑤⟩),𝑑𝑖𝑠𝑡[𝑤])
u is the newly added vertex to S adjacent to w
3. Add to S the vertex x not in S but of the minimum cost in
dist.
4. Repeat last two steps until S include all vertices.
62
Running Example
45
0 50 1 10 2
15 35
10 20 20 30
3 4 5
15 3
S 0 1 2 3 4 5
{0} 0 50 45 10 ∞ ∞
{0, 3} 0 50 45 10 25 ∞
{0, 3, 4} 0 45 45 10 25 ∞
{0, 3, 4, 1} 0 45 45 10 25 ∞
{0, 3, 4, 1, 2} 0 45 45 10 25 ∞
63
Running Example 2: 1/3
800 1200 1500
1 2 3 4
1000
250
300 1000 5
1400 900
1700 1000
0 7 6
S 0 1 2 3 4 5 6 7
S 0 1 2 3 4 5 6 7
Floyd-Warshall’s algorithm
◦ 𝐴−1 [𝑖][𝑗]: is just the 𝑙𝑒𝑛𝑔𝑡ℎ[𝑖][𝑗]
◦ 𝐴𝑛−1 [𝑖][𝑗]: the length of the shortest 𝑖-to-𝑗 path in
𝐺
◦ 𝐴𝑘 [𝑖][𝑗]: the length of the shortest path from 𝑖 to 𝑗
going through no intermediate vertex of index
greater than 𝑘.
◦ 𝐴𝑘 [𝑖][𝑗]= min{𝐴𝑘−1 [𝑖][𝑗], 𝐴𝑘−1 [𝑖][𝑘]+ 𝐴𝑘−1 [𝑘][𝑗] },
𝑘 ≥ 0
69
Floyd-Warshall’s Algorithm
There are only two possible paths for 𝐴𝑘 [𝑖][𝑗]!
◦ The path dose not pass vertex 𝑘.
◦ The path dose pass vertex 𝑘.
K-1
0 …
i j
K-1 K-1
0 … k 0 …
70
Example
6
𝑨−𝟏 0 1 2
0 1 0 0 4 11
4
11 1 6 0 2
3 2
2 3 ∞ 0
2
74
Floyd-Warshall’s Algorithm
1. void MatrixWDigraph::AllLengths(const int n)
2. {// length[n][n] stores edge length between
// adjacent vertices
3. // a[i][j] stores the shortest path from i to j
4. for (int i = 0; i<n; i++) 𝑶(𝒏)
5. for (int j = 0; j<n; j++) 𝑶(𝒏)
6. a[i][j]= length[i][j];
7.
8. // path with top vertex index k
9. for (int k= 0; k<n; k++) 𝑶(𝒏)
10. // all other possible vertices
11. for (int i= 0; i<n; i++) 𝑶(𝒏)
12. for (int j= 0; j<n; j++) 𝑶(𝒏)
13. if((a[i][k]+a[k][j])<a[i][j])
14. a[i][j] = a[i][k] + a[k][j];
15. }
Time complexity: 𝑶(𝒏𝟑) 75
Transitive Closure
The transitive closure matrix 𝑨+ :
◦ 𝑨+ is a matrix such that 𝑨+ [𝑖][𝑗] = 1 if there is a
path of length > 0 from 𝒊 to 𝒋 in the graph;
otherwise, 𝑨+ [𝑖][𝑗] = 0.
The reflexive transitive closure matrix 𝑨∗ :
◦ 𝑨∗ is a matrix such that 𝑨∗ [𝑖][𝑗] = 1 if there is a
path of length ≥ 𝟎 from 𝒊 to 𝒋 in the graph;
otherwise, 𝑨∗ [𝑖][𝑗] = 0.
Use Floyd-Warshall’s algorithm!
𝐴𝑘 𝑖 𝑗 = 𝐴𝑘−1 [𝑖][𝑗] || (𝐴𝑘−1 [𝑖][𝑘] &&𝐴𝑘−1 [𝑘][𝑗] )
76
6.4.4 Example: Transitive Closure
0 1 2 3
A+ 0 1 2 3 A* 0 1 2 3
0 0 1 1 1 0 1 1 1 1
1 0 1 1 1 1 0 1 1 1
2 0 1 1 1 2 0 1 1 1
3 0 0 0 0 3 0 0 0 1
Activity
Networks
Predecessor :
𝐶1 Vertex 𝑖 is a predecessor of
vertex 𝑗, iff there is a directed
𝐶0 𝐶3 path from vertex 𝑖 to vertex 𝑗.
𝐶2
79
Topological Order
A linear ordering of the vertices of a graph
such that, for any two vertices 𝑖 and 𝑗, if 𝑖 is
a predecessor of 𝑗 in the network, then 𝑖
precedes 𝑗 in the linear ordering.
𝐶1
𝐶0 → 𝐶1 → 𝐶2 → 𝐶3 (O)
𝐶0 𝐶3
𝐶0 → 𝐶2 → 𝐶1 → 𝐶3 (O)
𝐶2 𝐶0 → 𝐶2 → 𝐶3 → 𝐶1(𝑋)
80
Application
Course No. Course Prerequisites
C1 Programming I None
C2 Discrete Mathematics None
C3 Data Structures C1, C2
C4 Calculus I None
C5 Calculus II C4
C6 Linear Algebra C5
C7 Analysis of Algorithms C3, C6
C8 Assembly Language C3
C9 Operating Systems C7, C8
C10 Programming Languages C7
C11 Compiler Design C10
C12 Artificial Intelligence C7
C13 Computational Theory C7
C14 Parallel Algorithms C13
C15 Numerical Analysis C5 81
AOV Network of Courses
Use topological ordering to
generate a linear order list.
This list represents one possible
way to take all the courses 𝐶9
𝐶8 𝐶10 𝐶11
𝐶1 𝐶12
𝐶2 𝐶3 𝐶7 𝐶13 𝐶14
𝐶4 𝐶5 𝐶6 𝐶15
82
Topological Ordering
Iteratively pick a vertex v that has no
predecessors.
◦ Use an additional field “count” to record the “in-
degree” value of each vertex.
adjLists
0 [0] 1 2 3 0
1
1 [1] 4 0
0 2 4 1 [2] 4 5 0
1 [3] 5 4 0
3 5 3 [4] NULL
2 [5] NULL 83
Running Example
adjLists
1 0 [0] 1 2 3 0
1 [1] 4 0
0 2 4 1 [2] 4 5 0
1 [3] 5 4 0
3 5 3 [4] NULL
2 [5] NULL
Ordered list:
84
Running Example
adjLists
0 [0] 1 2 3 0
1
0 [1] 4 0
0 [2] 4 5 0
2 4
0 [3] 5 4 0
3 5 3 [4] NULL
2 [5] NULL
Ordered list: 0
85
Running Example
adjLists
0 [0] 1 2 3 0
1
0 [1] 4 0
0 [2] 4 5 0
2 4
0 [3] 5 4 0
5 2 [4] NULL
1 [5] NULL
Ordered list: 0 3
86
Running Example
adjLists
0 [0] 1 2 3 0
1
0 [1] 4 0
0 [2] 4 5 0
4
0 [3] 5 4 0
5 1 [4] NULL
0 [5] NULL
Ordered list: 0 3 2
87
Running Example
adjLists
0 [0] 1 2 3 0
1
0 [1] 4 0
0 [2] 4 5 0
4
0 [3] 5 4 0
1 [4] NULL
0 [5] NULL
Ordered list: 0 3 2 5
88
Running Example
adjLists
0 [0] 1 2 3 0
0 [1] 4 0
0 [2] 4 5 0
4
0 [3] 5 4 0
0 [4] NULL
0 [5] NULL
Ordered list: 0 3 2 5 1
89
Running Example
adjLists
0 [0] 1 2 3 0
0 [1] 4 0
0 [2] 4 5 0
0 [3] 5 4 0
0 [4] NULL
0 [5] NULL
Ordered list: 0 3 2 5 1 4
90
Self-Study Topics
Graph representations
◦ Sequential lists
◦ Adjacency multilists
Graph operation
◦ Biconnected components
Single source shortest path
◦ Bellman-Ford’s algorithm (Digraph with
negative edge costs)
Activity-on-Edge (AOE) Networks
◦ Critical path analysis
91