Escolar Documentos
Profissional Documentos
Cultura Documentos
Data Structures
Ching-Fang Hsu
Department of Computer Science and Information Engineering
National Cheng Kung University
Definitions
Y A directed graph
R Each edge is represented as a directed pair of
vertices.
R Ex. The pair <u, v> represents an edge in which u
is the tail and v is the head.
Y The restrictions on graphs
R No self loops (aka self edges; p. 268, Fig. 6.3(a))
R A graph may not have multiple occurrences of the
same edge.
X Multigraphs (p. 268, Fig. 6.3(b))
3
4
Definitions (contd.)
8
Definitions (contd.)
Y A cycle
R A simple path in which the first and the last vertices are the
same
Y Two vertices u and v are connected in an undirected
graph G if there is a path in G from u to v.
R An undirected graph is connected iff ∀ <u, v>, u ≠ v, ∃ a path
from u to v in G.
Y A connected component (≡ component) of an
undirected graph
R A maximal connected subgraph
R Ex. p. 270, Fig. 6.5
9
10
Definitions (contd.)
Y A tree
R A connected and acyclic graph
Y A directed graph is strongly connected iff for
every pair of distinct vertices u and v in V(G),
there is a directed path from u to v and also
from v to u.
Y A strongly connected component
R A maximal subgraph that is strongly connected
Y The degree of a vertex
R The number of edges incident to that vertex 11
Definitions (contd.)
14
Graph Representation --
Adjacency Matriices
Y A two-dimensional n × n array a
Y a[i][j] = 1 iff the edge (i, j) ∈ E(G); otherwise,
a [i][j] = 0.
R Ex. p. 272, Fig. 6.7
Y The main advantage
R Determining the degree of a vertex i is an −1simple task.
X For an undirected graph: the sum of row i ∑ a[i ][ j ]
j =0
X For a directed graph: the row sum ⇒ the out-degree
the column sum ⇒ the in-degree
15
16
Graph Representation --
Adjacency Matrices (contd.)
Y The main disadvantage
R All algorithms that need to examine all edges
require Ο(n2) time.
X n2 - n entries
X Examples
¹ How many edges are there in G?
¹ Is G connected?
X Not efficient for sparse graphs
17
Graph Representation --
Adjacency Lists
Y One chain for each vertex in G, instead of one
row of the adjacency matrix
Y The nodes in chain i represent the vertices that
are adjacent from vertex i.
Y For an undirected graph with n vertices and e
edges, an array of size n and 2e chain nodes are
required.
R Two fields in a chain node
R An alternative: sequentially packing the nodes on the
adjacency lists
18
Graph Representation --
Adjacency Lists (contd.)
X No pointers; an array node[]
¹ node[i]: the starting point of the list for vertex i, 0 ≤ i < n
¹ node[n]= n + 2e + 1
¹ The vertices adjacent from vertex i: node[i]~ node[i+1]-1
X Example: p. 275, Fig. 6.9
R For an undirected graph
X The degree of any vertex can be determined by counting
the # of nodes in its adjacency list.
X The total # of edges can be determined in Ο(n + e) time.
R For a digraph
X Out-degree ⇒ the total # of edges (in Ο(n + e))
19
20
Graph Representation --
Adjacency Lists (contd.)
X In-degree? More complex!
¹ Solution 1. -- Maintaining a second set of lists called
inverse adjacency lists
+ Ex. p. 275, Fig. 6.10
¹ Solution 2. -- Changing the node structure of the adjacency
lists
+ Ex. p. 276, Fig. 6.11 Lists in which nodes are shared among
several lists
21
22
Graph Representation --
Adjacency Multilists
Y For each edge, there is exactly one node, but
this node is on the adjacency list for each of
the two vertices it is incident to.
R p. 276
R Ex. p. 277, Fig. 6.12
23
24
Graph Representation -- Weighted
Edges
Y A graph with weighted edges is called a
network.
Y For an adjacency matrix
R Replace the 1 used to signify an edge with the
weight of the edge
Y For adjacency lists and adjacency multilists
R A weight field is needed!
25
Elementary Graph Operations
26
Elementary Graph Operations --
Depth First Search
Y Assume that the start vertex is v.
void dfs (int v)
{
nodePointer w;
visited[v] = TRUE;
printf (“%5d”, v);
for (w = graph[v]; w; w = w->link)
if (!visited[w->vertex])
dfs (w->vertex);
}
Y Time complexity
R Adjacency lists: Ο(e)
R An adjacency matrix: Ο(n2)
27
Set current = v
Visit current
Success
Set current = w Termination Set current = u
28
Elementary Graph Operations --
Breadth First Search
Y Assume that the start vertex is v.
R Visit each of the vertices on v’s adjacency list
R When all the vertices on v’s adjacency list have
been visited, visit all the unvisited vertices
adjacent to the first vertex on v’s adjacency list.
R p. 282, Program 6.2
R A dynamically linked queue is exploited.
Y Time complexity
R Adjacency lists: Ο(e)
R An adjacency matrix: Ο(n2)
29
30
Elementary Graph Operations --
Connected Components
Y Based on graph searches
Y Problem 1: Determine whether or not an
undirected graph is connected
R Call dfs(0) or bfs(0) and then determine if there are
any unvisited vertices
R Time complexity: Ο(n+e)
Y Problem 2: List the connected components of
a graph
R p. 283, Program 6.3
R Time complexity: Ο(n+e) for adjacency lists while
Ο(n2) for an adjacency matrix 31
32
Elementary Graph Operations --
Spanning Trees
Y What is a spanning tree?
R Any tree that consists solely of edges in graph G and
that includes all the vertices in G
R A minimal subgraph, G′, of G such that V(G′) = V(G)
and G′ is connected
R Given that |V(G)| = n, a spanning tree has n - 1 edges.
R p. 284, Fig. 6.17
Y How to create a spanning tree?
R A depth first spanning tree vs. a breadth first
spanning tree
R p. 285, Fig. 6.18 33
34
Elementary Graph Operations --
Biconnected Components
Y Assuming that G is an undirected graph
Y An articulation point
R A vertex v such that the deletion of v, together with all edges
incident on v, produces a graph G′ having at least two
connected components
Y A biconnected graph
R A connected graph that has no articulation points.
Y A biconnected component H of a connected graph G
R A maximal biconnected subgraph of G; G contains no other
subgraph that is both biconnected and properly contains H.
R No edge can be in two or more biconnected components of
a graph.
35
Elementary Graph Operations --
Biconnected Components &
Articulation Points (contd.)
Y How to find the biconnected components of G?
R Using any depth first spanning tree of G
R Example: p. 287, Fig. 6.19(a) and Fig. 6.20
X Depth first number of v (dfn(v))
¹ The sequence in which v is visited during the DFS
¹ Generally, if u is an ancestor of v in the depth first spanning tree,
then dfn(u) < dfn(v).
X A back edge (u, v)
¹ A nontree edge iff either u is an ancestor of v or v is an ancestor of u
¹ All nontree edges are back edges.
X A low value of a vertex u (low(u))
¹ The lowest depth dfn that we can reach from u using a path of
descendants followed by at most one back edge
¹ low(u) = min{dfn(u), min{low(w) | w is a child of u}, min{dfn(w} | (u, w)
is a back edge}} 36
Elementary Graph Operations --
Biconnected Components &
Articulation Points (contd.)
X A vertex u is an articulation point iff
¹ u is either the root of the spanning tree and has two or more
children, or
¹ u is not the root and u has a child w such that low(w) >= dfn(u).
X Example: p. 288, Fig. 6.21
X p. 289, Program 6.4 for dfn and low value determination
X p. 290, Program 6.6 for finding biconnected components of a
graph
¹ Assuming that the graph contains at least two vertices
37
38
39
40
41
42
Minimum Cost Spanning Trees
43
Minimum Cost Spanning Trees
(contd.)
X Construct an optimal solution in stages
X At each stage, make a decision that is the best (using
some criterion) at this time
X Make sure that the decision will result in a feasible
solution
X The selection of an item at each stage is typically based
on either a least cost (e.g., minimum spanning trees
problem) or a highest profit criterion.
R A feasible solution must satisfy the following
constraints
X We must use only edges within the graph
X We must use exactly n - 1 edges
X We may not use edges that would produce a cycle 44
Minimum Cost Spanning Trees
(contd.)
Y Three different algorithms are introduced.
R Kruskal’s algorithm
R Prim’s algorithm
R Sollin’s algorithm
45
Minimum Cost Spanning Trees --
Kruskal’s Algorithm
Y Build a minimum cost spanning tree T by
adding edges to T one at a time
R Step 1: Select the edges in nondecreasing order
of their cost
X Maintain the edges in E as a sorted sequential list for
more efficient processing
X Time complexity of sorting the edges in E: Ο(e log e)
X A min heap is ideally suited!
R Step 2: Check if an edge forms a cycle with the
edges that are already in T if it is added to T
X Use the union-find operations in Section 5.9
46
Minimum Cost Spanning Trees --
Kruskal’s Algorithm (contd.)
R Exactly n - 1 edges will be selected
Y p. 295, Program 6.7
R The union-find operations require less time than
choosing and deleting an edge.
⇒ The total time complexity is Ο(e log e).
Y p. 294~295, Fig. 6.22 and Fig. 6.23
47
48
49
50
Pick the next least
cost edge (v, w)
Yes No
Does (v, w) create
a cycle in T ?
No
Complete Add (v, w) to T
51
Minimum Cost Spanning Trees --
Prim’s Algorithm
Y Build a minimum cost spanning tree T by
adding edges to T one at a time
R At each stage, the set of selected edges forms a
tree.
X cf. Kruskal’s (a forest at each stage)
R Step 1: Begin with a tree T containing a single
vertex
X Any vertex in the original graph
R Step 2: Add a least cost edge (u, v) to T such that
T ∪ {(u, v)} is also a tree.
X Exactly one of u or v is in T.
52
Minimum Cost Spanning Trees --
Prim’s Algorithm (contd.)
Y p. 297, Program 6.8
R Time complexity: Ο(n2)
Y p. 298, Fig. 6.24
53
54
55
Pick a vertex arbitrarily
No
56
Minimum Cost Spanning Trees --
Sollin’s Algorithm
Y At the start of a stage, the set of selected
edges (E′), together with all n graph vertices,
form a spanning forest.
R The initial configuration: E′ = ∅
Y During a stage, select one edge for each tree
in the forest.
R A minimum cost edge that has exactly one vertex
in the tree
R Eliminate multiple copies of edges
Y p. 299, Fig. 6.25
57
58
A spanning forest
F = {Ti | Ti = {{vi}, ∅}, 0 ≤ i ≤ |V| -1}
Complete
59
Shortest Paths and Transitive
Closure
Y Assumptions
R The length of a path is defined as the sum of the
weights of the edges on that path.
R Directed graphs
R All weights are positive.
Y Three problems related to finding shortest
paths
R Single source all destinations
R All pairs shortest paths
R Transitive closure
60
Shortest Paths and Transitive Closure
-- Single Source/ All Destinations:
Nonnegative Edge Costs
Y Givens
R A directed graph, G = (V, E)
R A weighting function, w(e), w(e) > 0 ∀e∈E
R A source vertex, v0
R Let v0∈S, S⊆V and the shortest paths of all vertices
in S have been found. ∀w∉S, distance[w] is the
length of the shortest path starting from v0, going
through vertices only in S, and ending in w.
Y The goal
R Determine a shortest path from v0 to each of the
remaining vertices of G 61
Shortest Paths and Transitive Closure
-- Single Source/ All Destinations:
Nonnegative Edge Costs (contd.)
Y Example: p. 300, Fig. 6.26
Y Dijkstra’s algorithm
R Initial: S = {v0} and ∀i∈V, distance[i] = cost[v0][i],
⎧ w(< u, v >) < u, v >∈ E
⎪
cost[u ][v ] = ⎨∞ < u, v >∉ E and u ≠ v
⎪0 u=v
⎩
62
Shortest Paths and Transitive Closure
-- Single Source/ All Destinations:
Nonnegative Edge Costs (contd.)
R Step 2: ∀v∉S, if distance[v] > distance[u] + cost[u][v],
set distance[v] = distance[u] + cost[u][v]
R If the shortest paths of all vertices have been
found already, stop; otherwise, go to Step 1.
R p. 302~303, Program 6.9, Program 6.10
X Time complexity: Ο(n2)
63
64
65
Shortest Paths and Transitive Closure
-- Single Source/ All Destinations:
General Weights
Y Program 6.9 does not necessarily give the
correct results on graphs with negatively-
weighted edges.
R Ex. Fig. 6.29 on p. 305
Y When negative edge lengths are permitted,
we require that the graph have no cycles of
negative length.
R To ensure that shortest paths consist of a finite #
of edges
X Ex. Fig. 6.30 on p. 305
66
67
Shortest Paths and Transitive Closure
-- Single Source/ All Destinations:
General Weights (contd.)
Y With abovementioned assumption, there is a
shortest path between any two vertices of an
n-vertex graph that has at most n-1 edges on it.
Y dist l [u]: the length of a shortest path from the
source vertex v to vertex u under the constraint
that the shortest path contains at most l edges
R dist 1 [u] = length[v][u], 0 ≤ u < n
R dist n-1 [u]: the length of an undirected shortest path
from v to u
68
Shortest Paths and Transitive Closure
-- Single Source/ All Destinations:
General Weights (contd.)
Y So, our goal then is to compute dist n-1 [u] for all u.
R Using dynamic programming methodology
Y dist k [u] = min{dist k-1 [u], min{dist k −1[i ] + length[i ][u ]} }
i
R Ex. Fig. 6.31 on p. 306
Y Bellman and Ford algorithm
69
70
void BellmanFord (int n, int v))
{
for (int i = 0; i < n; i++)
dist[i] = length[v][i];
for (int k = 2; k <= n-1; k++)
for (each u such that u!= v and u has
at least one incoming edge)
for (each <i, u> in the graph)
if (dist[u] > dist[i]+length[i][u])
dist[u] = dist[i] + length[i][u]
}
71
Shortest Paths and Transitive Closure
-- Single Source/ All Destinations:
General Weights (contd.)
Y Program 6.11 on p. 307
R O(n3) when adjacency metrices are used
R O(ne) when adjacency lists are used
R Complexity reduction
X Terminate the for loop either after n-1 iterations or after
the first iteration in which no dist values are changed
X Maintain a queue of vertices i whose dist value changed
on the previous iteration of the for loop.
¹ The only values for i that need to be considered during the
next iteration
72
Shortest Paths and Transitive
Closure -- All Pairs Shortest Paths
Y Find the shortest paths between all pairs of
vertices, vi, vj, i ≠ j
Y Solution 1: Apply Dijkstra’s algorithm for n
vertices
R Time complexity: Ο(n3)
Y Solution 2: Use the dynamic programming
method
R The basic principle: Decompose a problem into
subproblems and each subproblem will be solved
by the same approach recursively
73
Shortest Paths and Transitive
Closure -- All Pairs Shortest Paths
(contd.)
R Ak[i][j]: The cost of the shortest path from i to j,
using only those intermediate vertices with an
index ≤ k.
X A-1[i][j] = cost[i][j]
X The cost of the shortest path from i to j is An-1[i][j].
R The basic idea is to begin with the matrix A-1 and
successively generate the matrices A0, A1, A2, …,
An-1.
74
Shortest Paths and Transitive
Closure -- All Pairs Shortest Paths
(contd.)
R Given Ak-1, ∀i and j, Ak[i][j] can be derived by
applying one of the following two rules
X Ak[i][j] = Ak-1[i][j]; does not go through the vertex with
index k
X Ak[i][j] = Ak-1[i][k] + Ak-1[k][j]
⇒ Ak[i][j] = min {Ak-1[i][j], Ak-1[i][k] + Ak-1[k][j]}, k ≥ 0
X No cycle with a negative length is allowed!
¹ p. 303,
X p. 309, Program 6.12
¹ Time complexity: Ο(n3)
75
76
Shortest Paths and Transitive
Closure -- Transitive Closure
Y Closely related to the all pairs, shortest paths
problem
Y Given a digraph G = (V, E), determine if there
is a path from i to j, ∀ i, j∈V
R Two cases are of interest
X The transitive closure of a graph
X The reflexive transitive closure of a graph
R Definition (The transitive closure matrix of a
digraph; A+)
⎧1 if there is a path of length > 0 from i to j
X A+ [i ][ j ] = ⎨
⎩0 otherwise 77
Shortest Paths and Transitive
Closure -- Transitive Closure
(contd.)
R Definition (The reflexive transitive closure matrix
of a digraph; A*)
⎧1 if there is a path of length ≥ 0 from i to j
X A [i ][ j ] = ⎨
*
⎩0 otherwise
⎩1 otherwise
81
82
Activity on Vertex (AOV) Networks
(contd.)
Y Definition (Predecessors)
R Vertex i in an AOV network G is a predecessor of
vertex j iff there is a directed path from i to j.
Vertex i is an immediate predecessor of vertex j iff
<i, j> is an edge in G.
Y Definition (Successors)
R If i is a predecessor of j, then j is a successor of i.
If i is an immediate predecessor of j, then j is an
immediate successor of i.
Y Ex. p. 316, Fig. 6.37
83
Activity on Vertex (AOV) Networks
(contd.)
Y If an AOV network represents a feasible
project, the precedence relations must be
both transitive and irreflexive.
Y Definition
R A relation ⋅ is transitive iff for all triples i, j, k, i ⋅ j
and j ⋅ k ⇒ i ⋅ k. A relation ⋅ is irreflexive on a set S
if i ⋅ i is false ∀i∈S.
Y We can show that a precedence relation is
irreflexive by proving that the network is a
directed acyclic graph (dag).
84
Activity on Vertex (AOV) Networks
(contd.)
Y Definition (Topological order)
R A topological order is a linear ordering of the
vertices of a graph such that, for any two vertices,
i, j, if i is a predecessor of j in the network then i
precedes j in the linear ordering.
Y Ex. p. 317
Y An algorithm to sort activities into topological
order
R p. 318, Program 6.13
R Ex. p. 318, Fig. 6.38
85
86
87
List out a vertex, v, in
the network that has
no predecessor
No
Yes
The project is infeasible 88
Activity on Edge (AOE) Networks
Y The directed edges represent tasks or
activities to be performed on a project.
Y The vertices represent events which signal
the completion of certain activities.
R Activities represented by edges leaving a vertex
cannot be started until the event at that vertex has
occurred.
Y “Start project”; “finish project”
Y A critical path is a path that has the longest
length from the start vertex to the finish vertex.
R A network may have more than one critical path. 89
Activity on Edge (AOE) Networks
(contd.)
Y The earliest time an event vi can occur = the
length of the longest path from the start
vertex 0 to i.
R Example: p. 322, Fig. 6.40
R It determines the earliest start time for all activities
represented by edges leaving that vertex.
X e(i) for activity ai; e(7) = e(8) = 7
Y The latest time of activity ai ≡ l(i)
R The latest time the activity may start without
increasing the project duration
X l(6) = 8, l(8) = 7 90
91
Activity on Edge (AOE) Networks
(contd.)
Y A critical activity
R An activity for which e(i) = l(i)
R l(i) – e(i)
X A measure of how critical an activity is
R Critical paths determination
X Delete all noncritical activities from the AOE networks
X Generate all the paths from the start to finish vertex
92
Activity on Edge (AOE) Networks
(contd.)
Y How to calculate e(i) and l(i), ∀i?
R Assume that activity ai is represented by edge <k,
l>.
e(i) = ee[k]
l(i) = le[l] – duration of activity ai
R It is easier to first obtain ee[j] and le[j], for all
events, j.
X A forward stage -- computing ee[j]
¹ Starting with ee[0] = 0
¹ ee[j] = maxi ∈P(j) {ee[i] + duration of <i, j>}
¹ P(j) – the set of all vertices adjacent to vertex j
93
Activity on Edge (AOE) Networks
(contd.)
X A backward stage – computing le[j]
¹ Starting with le[n – 1] = ee[n – 1]
¹ le[j] = mini ∈S(j) {le[i] - duration of <j, i>}
¹ S(j) – the set of vertices adjacent from vertex j
R Using the values of ee and of le to compute e [i]
and l [i] and the degree of criticality (also called
slack) for each task
94