TDDB57 DALG – Lecture 12: Graphs. Page 2

advertisement
TDDB57 DALG – Lecture 12: Graphs.
Page 1
J. Maluszynski, IDA, Linköpings Universitet, 2005.
TDDB57 DALG – Lecture 12: Graphs.
Page 2
Graphs
More Graph Algorithms
Lecture 12:
Lecture 14:
J. Maluszynski, IDA, Linköpings Universitet, 2005.
Graphs
[L/D 12.1]
Minimum Spanning Trees, Kruskal’s Algorithm
[L/D 12.3]
ADT Graph and its representation
[L/D 12.1] ]
Single Source Least Cost Paths
[L/D 12.3]
Tree characterizations, connected components
[L/D p. 431–432]
Graph traversals (DFS, BFS)
[L/D 12.2]
Lecture 13:
Biconnected Components
[L/D p. 432]
Topological sorting of directed acyclic graphs
TDDB57 DALG – Lecture 12: Graphs.
Page 3
[L/D 12.2]
J. Maluszynski, IDA, Linköpings Universitet, 2005.
TDDB57 DALG – Lecture 12: Graphs.
Page 4
J. Maluszynski, IDA, Linköpings Universitet, 2005.
Motivation
Graphs
Graph: points (nodes / vertices) and connecting lines / arrows (edges).
Connectivity (topology) does matter, but layout does not
Used very often to model real life situations, for example:
road / railroad / direct flight connections between cities
undirected graph (directed if one-way roads occur)
The world-wide web
directed graph, documents connected by hyperlinks
e
f
a
a
g
e
f
g
h
h
c
d
prerequisites for taking courses
directed graph, courses and direct precedence constraints
c
Intermediate program representation in a compiler
directed graph, operations connected by control / data flow edges
Electric Circuits
undirected graph, electrical components connected by wires,
wires labeled e.g. with wire capacities
b
b
a
b
d
c
e
d
f
h
g
TDDB57 DALG – Lecture 12: Graphs.
Page 5
J. Maluszynski, IDA, Linköpings Universitet, 2005.
TDDB57 DALG – Lecture 12: Graphs.
Page 6
J. Maluszynski, IDA, Linköpings Universitet, 2005.
Let
Neighbourhood/Adjacency of vertices:
(or
): and are neighbors in
for undirected graphs:
Directed graph (digraph)
edges are ordered pairs
finite set of edges
Sometimes we assume
– no self-loops / trivial cycles
Degree of vertices
for directed graphs:
Undirected graph
edges are unordered pairs
finite set of edges
be a finite, non-empty set of vertices (or nodes)
Graph terminology
Graphs
Often, additional information is added to vertices and/or edges.
TDDB57 DALG – Lecture 12: Graphs.
Page 7
J. Maluszynski, IDA, Linköpings Universitet, 2005.
ADT Graph
TDDB57 DALG – Lecture 12: Graphs.
Page 8
J. Maluszynski, IDA, Linköpings Universitet, 2005.
Representation of Graphs
Graph
with vertices
0/ 1
/
. 1
Adjacency matrix: a boolean
matrix
9
8
:
76
5
342
if
otherwise
and all edges to and from
8
Adjacency lists:
for each store a list of neighbors / vertices adjacent to
8
delete
, no edges
& " & $ - !% % $ # &
( $ % " * + , ,
# & ' ) ) * *
return new graph with vertices
return vertices of the graph
2
...
1 2 3 4 5
1
4
3
5
1
2
3
4
5
0
0
0
1
0
1
0
0
0
0
1
1
0
0
0
0
1
0
0
0
0
1
1
0
0
1
2
3
4
5
2
3
5
1
3
4
5
TDDB57 DALG – Lecture 12: Graphs.
Page 9
J. Maluszynski, IDA, Linköpings Universitet, 2005.
7
//
/// :
3
8 ///
.8 8 ,
1
3
4
5
2
2
A cycle in is a path
in
such that
, and successive edges are different.
1
4
is cyclic if it has a cycle; otherwise it is acyclic.
Page 11
Path:
<1 5 3 2 1 4>
Simple path: <1 4>
Cycle:
<1 5 4 1 2 3 5 1>
Simple cycle: <1 5 4 1> = < 5 4 1 5> =...
.
A path in is simple if all vertices are distinct,
except that may be equal to .
Then, is called the length of the path.
TDDB57 DALG – Lecture 12: Graphs.
J. Maluszynski, IDA, Linköpings Universitet, 2005.
2
be an undirected graph.
A path in is a sequence of vertices
where
is an edge for
(path from to )
Page 10
Undirected Graphs: Example for terminology
Undirected Graphs: Paths and Cycles (1)
Let
TDDB57 DALG – Lecture 12: Graphs.
J. Maluszynski, IDA, Linköpings Universitet, 2005.
Undirected Graphs: Connectivity, Treeness, Completeness
TDDB57 DALG – Lecture 12: Graphs.
Notice: root not distinguished.
3
4
5
5
Page 12
J. Maluszynski, IDA, Linköpings Universitet, 2005.
Trees: Changing the root
D
is connected iff there is a path between any two vertices.
is a (free) tree iff there is a unique simple path between any two vertices
in
1
3
A
A
D
E
G
F
is complete if every two vertices are connected by an edge.
B
G
D
C
A
B
G
F
C
B
F
E
C
E
TDDB57 DALG – Lecture 12: Graphs.
Page 13
J. Maluszynski, IDA, Linköpings Universitet, 2005.
1
7
1
A connected graph with
is a subgraph of
if
, and
vertices has at least
edges.
Lemma (b)
vertices has at most
edges.
.
Proof: ...
where maximal means: if
is a connected component of ,
then
is not a subgraph of any larger connected subgraph of
1
is a maximal connected subgraph of
7
1
An acyclic graph with
A connected component of
TDDB57 DALG – Lecture 12: Graphs.
Page 15
1
with
vertices.
Theorem:
(Tree characterization theorem, part II)
A graph
is a tree if any of the following holds:
1.
is connected and acyclic, or
2.
is connected, but deleting any edge makes it disconnected, or
3. adding any edge to yields a graph with
exactly one simple cycle containing the edge.
is acyclic;
J. Maluszynski, IDA, Linköpings Universitet, 2005.
2.
Page 16
Theorem:
(Tree characterization theorem, part I)
If is a (free) tree, then has the following properties:
is connected;
TDDB57 DALG – Lecture 12: Graphs.
Sufficient Conditions for Trees
Given an undirected graph
1.
.
J. Maluszynski, IDA, Linköpings Universitet, 2005.
Properties of Trees
5.
is acyclic and has exactly
edges.
Proof: ...
Explain why!
Assume a graph
has one of these properties. Is
a tree?
1
is connected and has exactly
1
1
7
has exactly
4.
7
4. If vertices , are not connected by an edge
then adding the edge creates one simple cycle;
yields a disconnected graph.
7
3. Deleting any edge from
5.
J. Maluszynski, IDA, Linköpings Universitet, 2005.
Lemma (a)
be an undirected graph.
A graph
Page 14
Undirected graphs: Number of Edges
Subgraph, Connected components
Let
TDDB57 DALG – Lecture 12: Graphs.
edges.
edges.
TDDB57 DALG – Lecture 12: Graphs.
Page 17
J. Maluszynski, IDA, Linköpings Universitet, 2005.
Page 18
J. Maluszynski, IDA, Linköpings Universitet, 2005.
Graph Searching: Reachability
Graph Searching
Search of a graph
starting at a vertex :
recursively visit all nodes reachable from
Mark all vertices unencountered.
Choose vertex , mark it encountered
.
there is a vertex
;
edge
to an unencountered neighbor
mark as encountered
Alternatively, the edges used could be marked instead of the vertices.
TDDB57 DALG – Lecture 12: Graphs.
Page 20
J. Maluszynski, IDA, Linköpings Universitet, 2005.
For an undirected graph:
For non-weighted edges, the distance between and
is the minimum number of edges traversed to get from
Breadth-First Search
Graph searching: Connected components
When
from
to
.
becomes empty, some vertices may still not have been encountered:
new CC!
Breadth first search starting at :
select vertices by increasing distance from
Realize as a queue.
Repeat the search for all vertices that have not yet been encountered
Some ordering is assumed on vertices of equal distance.
2 b
1 a
6 d
? How to select vertices in a systematic way?
J. Maluszynski, IDA, Linköpings Universitet, 2005.
Page 19
How to explore an unknown graph from a single vertex?
...
here, encountered vertices:
there is a path from to
here, : encountered but not yet visited vertices
All connected nodes are reached: induction over distance of
Optimization problems (e.g., scheduling or resource allocation):
precedences / interdependences often represented as a graph
solved by systematic exploration of all nodes
Routing / Motion planning in robotics
map represented as a graph, find a feasible path
% Applications:
by following edges
+ reachability from a vertex (path existence)
+ finding a shortest path, ...
+ connected components, biconnected components of an undirected graph
+ strongly connected components of a directed graph
TDDB57 DALG – Lecture 12: Graphs.
TDDB57 DALG – Lecture 12: Graphs.
f 5
3
c
e 4
a
b
c
d
e
f
b
a
a
e
c
b
c
c
b
d
f
e
Breadth-first search starting at a
TDDB57 DALG – Lecture 12: Graphs.
Page 21
J. Maluszynski, IDA, Linköpings Universitet, 2005.
TDDB57 DALG – Lecture 12: Graphs.
Page 22
J. Maluszynski, IDA, Linköpings Universitet, 2005.
Application of Breadth-First Search: Finding the Shortest Path
Breadth-First Search
5 2 25
"$ % $ $ ! " ! & % & *
% &
(
,
2 b
)
false;
;
Check existence of a path between a and f:
f 4
1 a
c
S:
a
c,b
f,c
e,f
e
3
true
5
2
25 $ $ &
maybe do some operation on
neighbor of
6 d
e 5
Encountered:
a
a,b,c
a,b,c,f
a,b,c,f,e
a,b,c,f,e
Visited:
a
a,b
a,b,c
a,b,c,f
Length:
0
1
1
2
A path of length 2 exists !
here
true
(
)
1
Time complexity
. Explain!
TDDB57 DALG – Lecture 12: Graphs.
Page 23
J. Maluszynski, IDA, Linköpings Universitet, 2005.
TDDB57 DALG – Lecture 12: Graphs.
Page 24
J. Maluszynski, IDA, Linköpings Universitet, 2005.
Depth-first search
as above, but
for each neighbor of :
continue to depth-first search at , if not yet done
stack needed to keep unexplored neighbors.
Use the implicit stack of recursive function calls to implement
Some ordering on neighbors is assumed.
Depth-First Search, recursive formulation
d
true
c
e
neighbor
Depth-first search starting at a
abfced
before visiting the neighbors
some other operation on
after visiting the neighbors
(Breadth-first search starting at a) a b c f e d
some operation on
of
c
f
b
)
e 5 (5)
b
c
d
e
f
b
a
a
e
c
b
(
*
6 (6) d
c 4 (3)
a
)
;
1 (1) a
f 3 (4)
,
false;
5
2
5
2 $ *
% %
$ ! %!
2 (2) b
(
25
$" $
% " !
* *
Depth-First Search
Download