11. Intro to Graphs

advertisement
Discrete Maths
242-213, Semester 2, 2015-2016
11. Graph Theory
• Objective
 introduce graph theory (e.g. Euler and Hamiltonian cycles), and
discuss some graph algorithms (e.g. Dijkstra’s shortest path).
1
1. Graph Terms
 Vertices/Nodes
 (Strongly) Connected Component
 Edges
 Sub Graph
 Un/Weighted
 Complete Graph
 Un/Directed
 Directed Acyclic Graph (DAG)
 In/Out Degree
 Euler/Hamiltonian Cycle
 Self-Loop/Multiple Edges
 Sparse/Dense
 Path, Cycle
 Isolated, Reachable
Contest Algorithms: 6. Graph Intro
2
 A graph has two parts (V, E), where:
 V are the nodes, called vertices
 E are the links between vertices, called edges
SFO
PVD
ORD
LGA
HNL
LAX
DFW
MIA
 Directed graph
 the edges are directed
 e.g., bus cost network
 Undirected graph
 the edges are undirected
 e.g., road network
• A weighted graph adds edge numbers.
8
a
4
b
6
2
6
c
3
5
d
4
9
e
12
 End vertices (or endpoints) of an edge
 U and V are the endpoints
 Edges incident on a vertex
 a, d, and b are incident
a
 Adjacent vertices
 U and V are adjacent
 Degree of a vertex
 X has degree 5
 Parallel edges
 h and i are parallel edges
 Self-loop
 j is a self-loop
V
b
d
U
h
X
c
e
Z
i
g
W
f
Y
j
 Path
 sequence of alternating vertices and edges
 begins with a vertex
 ends with a vertex
 each edge is preceded and followed by its
endpoints
 Simple path
 path such that all its vertices and edges are
distinct
a
U
c
V
b
d
P2
X
e
g
W
 Examples
 P1=(V,b,X,h,Z) is a simple path
 P2=(U,c,W,e,X,g,Y,f,W,d,V) is a path that is
not simple
P1
f
Y
h
Z
 Cycle
 circular sequence of alternating vertices and edges
 each edge is preceded and followed by its endpoints
 Simple cycle
 cycle such that all its vertices and edges are distinct
 Examples
 C1=(V,b,X,g,Y,f,W,c,U,a) is a simple cycle
 C2=(U,c,W,e,X,g,Y,f,W,d,V,a,) is a cycle that is not
simple
Graphs
a
U
c
V
b
d
C2
X
e
C1
g
W
f
h
Y
8
Z
 A graph is connected if there is a
path between every pair of vertices
Connected graph
Non connected graph with two
connected components
Graphs
Strong Connectivity
 Each vertex can reach all other vertices
a
g
c
d
e
f
b
10
Complete Graphs
 All pairs of vertices are connected by an edge.
 No. of edges |E| = |V| (|V-1|)/2 = O(|V|2)
11
Directed Acyclic Graph (DAG)
 A DAG has no cycles
 Some algorithms become simpler when used on DAGs instead of
general graphs, based on the principle of topological ordering
 find shortest paths and longest paths by
processing the vertices in a topological order
Contest Algorithms
12
2. The Euler Cycle
• If a graph G has a simple cycle from vertex
v to v, which uses every edge exactly once,
only if there are an even number of
edges connected to each vertex.
• The first graph theory result, proved by by
Leonhard Euler in 1736 for the Konigsberg
Bridge Problem.
13
Practical Uses of Euler Cycles
• In computer networks, edge traversal
(i.e. moving between network nodes) is expensive.
• Euler’s definition is a very fast algorithm for checking whether a graph
(network) can be traversed efficiently.
14
• Can this network be traversed
Example
a
b
efficiently (e.g. by a Web search
engine collecting information)?
c
• e.g. start at a, finish at a, travel
f
e
d
g
h
i
each edge only once?
15
3. Hamiltonian Cycle
• Sir William Rowan Hamilton’s puzzle (1850’s)
 it made him very rich
• Each corner is labelled with a city name.
• The shape is a dodecahedron.
My version
uses country
names.
continued
16
• Problem: start at any city (letter), travel along the edges, visit each city
exactly once, and return to the starting city.
• Note: not all edges need to be used
17
Graph of Hamilton’s Puzzle
18
Hamiltonian Cycle Formalised
• In a graph G, find a cycle that contains each vertex exactly once,
except for the starting/ending vertex that appears twice.
19
A Solution
b
a
g
f
h
p
t
o
e
not all
edges used
n
r
s
m
i
q
j
c
k
l
d
20
Hamilton vs. Euler?
• A Euler cycle visits each edge once.
• A Hamiltonian cycle visits each vertex once.
• They sound similar, but mathematicians have much harder problems
with Hamiltonian cycles
 e.g. it is easy to check for a Euler cycle, but there is no simple test for a
Hamiltonian cycle
21
Algorithms for Finding Cycles
• There are algorithms for finding a Euler cycle in a graph which take O(a)
time
 a is the number of edges in the graph
• Algorithms for finding a Hamiltonian cycle are O(ea) or O(a!) in the worst
case
 much too slow for real graphs
continued
22
• For that reason, algorithms designed for real-world data only
generate near-minimum length cycles
 they are less time consuming, but may not give the best answer
23
Some Properties of Hamiltonian Cycles
• 1) If a graph has N verticies, then the Hamiltonian cycle must use N
edges.
 e.g.
s
a
b
w
t
v
d

c
u
2) Every vertex in a Hamiltonian cycle has a degree of 2 (some
edges may not be used).
24
Proving there is no H.C
• The graph has 5 verticies and 6 edges. v4
v1
and v2 have degree 3.
v2
v5
v4
• Must include the two edges connected to
v3
v1 and v3.
• This creates a loop, but without v5, so not
a H.C.
continued
25
Proving there is no H.C
b
a
e
d
g
c
f
i
h
j
k
l
m
continued
26
• Assume that the graph does have a H.C.
• Edges (a,b), (a,g), (b,c), (c,k) must be in the H.C. since verticies ‘a’, ‘c’ have
degree 2.
• Therefore, edges (b,d), (b,f) must not be in the H.C. since ‘b’ is fully used
already.
continued
27
• Therefore, edges (g,d), (d,e), (e,f), (f,k) must be in the H,C,. since that is the
only way to have ‘d’ and ‘f’ in the H.C.
• But there is now a cycle:
 {a, b, c, k, f, e, d, g, a}
• We cannot connect any more edges to g, e, k since their degree is 2 already
 so it is not possible to create a H.C.
28
4. The Travelling Salesman Problem
• This problem is related to the Hamiltonian cycle, but the graph is
weighted
 see the sheet metal hole drilling example
• Given a weighted graph G, find a minimum length Hamiltonian cycle in
G.
29
Example
a
b
2
11
3
d
3
3
• Answer: {a,b,c,d,a} with minimum length
11.
• Proof: try replacing any edge (e.g. (d,c) by
either of the ‘long’ edges.
c
11
30
Why Travelling Salesman?
• Think of the verticies as cities, edge weights as distances.
• The problem becomes: find a shortest route in which a salesman (or
woman) can visit each city once, starting and ending at the same city.
31
5. Depth First Search (DFS)
• Depth First Search (DFS) is a graph searching method, useful for
directed graphs
 e.g. the Web
• DFS uses recursion to explore all the successors of a node, and
backtracking to choice points.
• Problem: cycles
• Solution: DFS ‘marks’ nodes as it visits them, and never revisits marked
nodes.
32
• DFS is “depth first” because it always fully explores down a path away from
a vertex v before it backtracks to looks at other paths (other choices)
leaving v.
33
Directed Graph Example
a
b
d
e
f
c
Graph G
34
DFS Tree
• Since nodes are marked, the graph is searched as if it were a tree:
a/1
b/2
c/3
d/4
e/5
f/6
c
35
Running Time
• The time taken to search from a node is proportional to the no. of
successors of that node.
• Total search time for all nodes = O(n).
Total search time for all successors = time to search all edges = O(a).
• Total running time is O(n + a)
continued
36
• If the graph is dense, a >> n, the O(n) term can be ignored
 in that case, the total running time = O(a)
37
Uses of DFS
• Finding cycles in a graph
 e.g. for finding recursive dependencies in a calling graph
• Reachability detection
 i.e. can a vertex v be reached from vertex u?
 useful for e-mail routing
38
6. Finding the Shortest Path
• A weighted graph has values (weights)assigned to its edges.
• The length of a path = the sum of the weights of the edges in the path
 w(i,j) = weight of edge (i,j)
• The shortest path between two verticies = the path having the
minimum length.
39
Example Weighted Graph
2
b
2
4
2
d
a
1
c
4
3
3
1
7
f
5
z
e
6
g
Problem: find the shortest path from vertex a to vertex z.
40
Dijkstra’s Shortest Path Algorithm
• Assign scores to verticies:
 S(v) = score of vertex v (some integer)
 there are temporary and permanent scores
 all verticies start with a temporary score of infinity (Inf)
continued
41
• The algorithm uses a set T, which contains all the nodes with
temporary scores
 initially that is all n nodes
• When the score of a vertex v is made permanent, it is also the length
of the shortest path from vertex a to vertex v
 this is what we want!
42
Algorithm (as C-like pseudocode)
int dijkstra(vertex a, vertex z)
// find the shortest path from a to z
{
// initialisation
S(a) = 0;
// 1
for (all verticies x != a)
// 2
S(x) = Inf;
// 3
T = all verticies;
:
// 4
continued
43
// find shortest path to z
while (z in T) {
choose v from T with min S(v);
T = T without v;
for (each x in T adjacent to v)
S(x) = smaller_of(
S(x), S(v)+w(v,x) );
}
return S(z);
// 5
// 6
// 7
// 8
// 9
// 10
// return shortest path
}
44
Processing the Example
2
b
2
4
2
d
a
1
c
4
3
3
5
z
e
7
f
1
6
g
continued
45
Initialisation
b
2
0
Inf
1
c
4
2
Inf
d
a
2
4
3
Inf
3
5
1
z
e Inf
7
f
= temporary
score
Inf
6
g Inf
Inf
continued
46
= permanent
score
First Iteration
b
2
0
2
1
c
4
2
Inf
d
a
2
4
3
Inf
3
5
1
z
e Inf
7
f
= changed
temporary
Inf
6
g Inf
1
continued
47
Second Iteration
b
2
0
2
1
c
4
2
4
d
a
2
4
3
Inf
3
5
z
e Inf
7
f
1
Inf
6
g 6
1
continued
48
Third Iteration
2
b
2
0
1
c
4
2
4
d
a
2
4
3
4
3
e 6
7
5
f
1
z
Inf
6
g 6
1
continued
49
Fourth Iteration
2
b
2
0
1
c
4
2
4
d
a
2
4
3
3
5
1
1
e 6
7
f
4
z
5
6
g 6
Could have chosen
‘d’ instead.
continued
50
Fifth Iteration
2
b
2
0
2
4
2
4
d
a
1
c
4
3
3
5
1
1
e 6
7
f
4
g 6
z
5
6
Choosing ‘d’ is a
waste of time.
continued
51
Sixth (and final) Iteration
2
b
2
0
2
4
2
4
d
a
1
c
4
3
3
5
1
1
e 6
7
f
4
g 6
z
5
6
Finished!
Shortest path
from ‘a’ to
‘z’ is length 5.
52
Notes
• On each iteration:
 one score becomes permanent
 the vertex with the new permanent score changes its adjacent verticies’
temporary scores if they can be made smaller
• The algorithm eventually reaches every vertex.
53
Execution Time (worst case)
• The graph has n verticies.
• T is the set of verticies with temporary scores
 initially it contains all n verticies
54
Consider the Algorithm
• The loop on lines 2-3 is executed n-1 times
 it has O(n) execution time
• The loop on lines 5-10 is executed n times.
• The choose statement on line 6 will require a search through all of T
in the worst case:
 so it has O(n) execution time
continued
55
• The loop on lines 8-9 looks through all of T:
 O(n) execution time
• Total execution time of loop on lines 5-10:
 O(n * (n+n))
= O(n2)
• Total execution time of function:
 O(n) + O(n2)
= O(n2), for large n
56
7. Further Information
• Discrete Mathematics and its Applications
Kenneth H. Rosen
McGraw Hill, 2007, 7th edition
• chapter 10, sections 10.1, 10.2, 10.5, 10.6
57
Download