Model A Wide Variety of Phenomena: Graphs Graph Algorithms
Model A Wide Variety of Phenomena: Graphs Graph Algorithms
Terminology:
A graph consists of a set of nodes and set of edges. Edges link
pairs of nodes. For directed graphs each edge has a source node
and a target node. Nodes linked by an edge are said to be adjacent
(not ‘connected’- this is used for a more general concept).
target(ei ) = source(ei+1 ).
For undirected graphs, paths are similar but with the requirement
that adjacent edges in the sequence are incident at the same node.
s p
H k h
g
q
G D B
r
t
I
E
F
s p
H k h
g
q
G D B
r
t
I
E
F
A B C D E F G H I J
A 0 0 0 0 1 0 0 0 0 0
B 0 1 0 0 0 0 0 0 0 0
C 1 0 0 0 0 0 0 0 0 0
D 0 0 0 0 0 0 0 0 0 0
E 0 0 1 0 0 0 0 0 0 0
F 0 0 0 0 0 0 1 0 0 0
G 0 0 1 0 0 1 0 0 0 0
H 0 0 0 0 0 0 0 0 0 1
I 0 0 0 0 0 0 0 0 0 0
J 0 0 0 1 0 0 0 0 0 0
Traversal idea: Visit every node following the structure of the tree.
u := root;
s := empty;
i := 0;
push(u,s);
while s not empty do
{ i := i+1;
search-num(top(s)) := i;
u := top(s);
pop(s);
forall v children of u
push(v,s) }
Tranversal techniques: Graphs
visit(u) =
{ i := i+1;
dfsnum(u) := i;
forall nodes v adjacent to u
if dfsnum(v) = 0 then visit(v) };
forall nodes u
(if dfsnum(u) = 0 then visit(u));
Analysis of DFS
Applications?
Articulation points
Planarity:
An embedding of a graph in the plane is an allocation of
distinct points to the nodes and distinct continuous lines (not
necessarily straight - that is another problem) to the edges, so
that no two lines intersect.
Some graphs can be embedded, others not. Hopcroft and
Tarjan introduced the first linear-time planarity algorithm
(1974) based on Depth-First Search.
Subtrees in graphs: A spanning tree of an undirected graph is
a subgraph which (a) is a tree (i.e. no cycles), and (b)
includes all nodes. For integer-labelled graphs, a minimum
spanning tree has minimum label-sum.
Finding minimum spanning trees is an optimisation problem
for which there are greedy solutions with worst-case time
complexity O(E × log(N)).
Survey of some computation tasks on graphs: 3
N 2N N! NN
10 103 4 × 106 1010
20 106 2 × 1018 1026
100 1030 9 × 10157 10200
1, 000 10301 102672 103000
10, 000 103000 1036701 1040000
The proof of this gives a linear-time algorithm for testing for, and
constructing, an Eulerian circuit.
Complexity classes and computational tasks (continued)
n := 1;
while n =< N do
{ attempt to colour node n with
next colour not tried for n }
if there is no such colour
then
if n>1 then n := n-1 else fail
else if n=N then print colouring else n := n+1
Stirling’s formula:
√
N! ≈ 2πN(N/e)N
Why?
Part 2. Complexity