241-303 discrete maths: graphs/5 1 discrete maths objective – –introduce graph theory (e.g....

138
1-303 Discrete Maths: Graphs/5 Discrete Maths Objective introduce graph theory (e.g. Eul er and Hamiltonian cycles), and discuss some graph algorithms (e .g. Dijkstra’s shortest path). 241-303, Semester 1 2014-2015 5. Graph Theo ry

Upload: rosanna-mason

Post on 20-Jan-2016

267 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 1

Discrete Maths

• Objective– introduce graph theory (e.g. Euler and Ha

miltonian cycles), and discuss some graph algorithms (e.g. Dijkstra’s shortest path).

241-303, Semester 1 2014-2015

5. Graph Theory

Page 2: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 2

Overview

1. Introduction

2. Cycles

3. Hamiltonian Cycles

4. Algorithms for Finding Cycles

5. Similarity Graphs

6. Implementing Graphs

continued

Page 3: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 3

7. Depth First Search

8. Finding the Shortest Path

9. Planar Graphs

10. More Information

Page 4: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 4

1. Introduction

Muddy Gap

CasperDouglas

GillettteBuffalo

SheridanGreybull

Worland

Shoshoni

Lander

Part of Wyoming’s (a USA State) Road System

Page 5: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 5

Problem

• The Wyoming Road Inspector lives in Greybull, and must check every road.

• He must check the roads as quickly as possible– by travelling each road only once– starting from Greybull and returning there

• Is this travel plan possible?

Page 6: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 6

(Undirected) Graph Version

Lan

Sho

Wor

Mud

Cas Dou

GilBuf

SheGre

e1

e2e4

e3

e6 e7e9

e11

e12

e13

e10

e5e8

continued

Page 7: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 7

• Dots = vertices/nodes (singular: vertex)• Lines = edges/arcs• An undirected graph is one where the edges

have no direction (no arrows) on them.

Page 8: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 8

Equivalent Graph

Cas

Sho

Lan

Mud

WorGre

SheBuf

Gil

Dou

e9

e6

e12 e4 e1

e2

e3

e7

e8 e5e10

e13

e11

Page 9: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 9

1.1. Paths

• A path is a sequence of edges between two verticies– e.g. the path from Sheridan (She) to Muddy Gap (

Mud) is: {She, Buf, Cas, Mud}

• Road Inspector Problem:– is there a path from Gre (Greybull) to Gre which

uses all the edges only once?

Page 10: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 10

Answer: No

• Consider Worland (Wor): the inspector must use every edge connected to Wor only once.

Sho

WorGre

Buf

e6

e4

e2

continued

Page 11: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 11

• But to travel through Wor requires 2 edges (one in, one out).

• So there is no way to use the third edge to visit Wor without using one of the other edges again.

Page 12: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 12

The General Theory

• If a graph G has a path from vertex v to v, which uses every edge exactly once, then an even number of edges must be connected to each vertex.

• More on this in section 2.

Page 13: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 13

1.2. A Directed Graph

• A directed graph = vertices/nodes and arcs.• An arc = a directed edge (one with an arrow).

v5

v2

v1

v3

v6v4e2

e1 e3

e6

e5e4

e7

Page 14: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 14

A Calling Graph• A Calling Graph for a small program:

main

printList

mergeSort

mergesplit

makeList

4 examples ofdirect recursion

Page 15: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 15

1.3. Parallel Edges, Loops, Isolation

• A graph with parallel edges, a loop, and an isolated vertex:

v1

v3

v4v2

e1

e2

e4

e3

continued

Page 16: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 16

• Parallel Edges e1, e2 = (v1, v2)A loop e3 = (v2, v2)Isolated (or unconnected) vertex: v4

• A graph with no loops and no parallel edges is a simple graph.

• A graph with no isolated verticies is a connected graph.

Page 17: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 17

1.4. Path Length

• The length of a path = the number of edges that it uses.

• If edges/arcs are labeled with numbers then we can sum the values along a path to get a “distance”.

Page 18: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 18

Sheet Metal Hole Drilling

• Problem: minimise the moving time of the drill over the metal sheet.

continued

Page 19: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 19

A Weighted Graph Version

• Add edge numbers (weights) to indicate the movement time between any two holes.

129

4

4

35

6

8

6a

d

e

c

b

2

Page 20: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 20

Simplified Problem

• Assume that the drilling must start at vertex ‘a’ and end at vertex ‘e’.

• What is the path with the shortest length between ‘a’ and ‘e’?– length = sum of weights on the path’s edges

Page 21: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 21

Listing all Possibilities

• Path Lengtha,b,c,d,e 21a,b,d,c,e 28a,c,b,d,e 24a,c,d,b,e 26a,d,b,c,e 27a,d,c,b,e 22

The shortestlength path.

Page 22: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 22

Is there a Better Way?

• Finding an answer by listing all the paths is very time consuming for real problems.

• But there is no known approach/algorithm which is faster for general graphs!– finding a better algorithm would be a major bre

akthrough in maths (and engineering)

Page 23: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 23

2. Cycles

• In a directed graph, a cycle is a path that begins and ends at the same node.– e.g. {Gre, She, Buf, Wor, Gre}

• A simple cycle is a path with no repeated verticies, except the ends.– e.g. {Wor, Buf, Gil, Buf, Wor}

is not a simple cycle

continued

Page 24: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 24

• In an undirected graph, a simple cycle must have two or more different nodes.– e.g. the cycle {Wor, Wor} is excluded

2 nodes

Page 25: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 25

2.1. Konigsberg Bridge Problem

continued

The bridges are red.

Page 26: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 26

The Bridges from Above

• Problem: walk over each bridge exactly once; ending back at the starting point.

• A classic example of a general graph problem.• Solved by Leonhard Euler in 1736.

D

A

B CBriver

Page 27: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 27

Graph Model

• Find a cycle in the graph that includes all the edges and all the vertices– but use each edge exactly onc

e

• Called a Euler cycle.

A

C

D

B

Page 28: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 28

A Solution

• Use the same technique as in the Wyoming road inspection problem.

• Consider vertex A: since it has 3 edges connected to it, then the graph cannot have a Euler Cycle.

Page 29: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 29

2.2. Euler Cycle Formalised

• A graph G has a Euler cycle when all the nodes are connected and every vertex has an even degree.

• The degree of a vertex v = number of edges connected to v.

Page 30: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 30

2.3. Practical Uses

• 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.

Page 31: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 31

Example • Can this network be traversed efficiently (e.g. by a Web search engine collecting information)?

• e.g. start at a, finish at a, travel each edge only once?

a b c

def

g h i

Page 32: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 32

3. Hamiltonian Cycles

• 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.

continued

My versionuses countrynames.

Page 33: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 33

• 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

Page 34: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 34

Graph of Hamilton’s Puzzle

a b

c

d

e

fg

h

i

j

kl

mn

op

qrs

t

Page 35: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 35

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.

Page 36: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 36

A Solution

a b

c

d

e

fg

h

i

j

kl

mn

op

qrs

t

not all edges used

Page 37: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 37

3.1. 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

Page 38: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 38

3.2. Some Properties of Hamiltonian Cycles

• 1) If a graph has N verticies, then the Hamiltonian cycle must use N edges.– e.g.

a b

cd

s

t

u

vw

2) Every vertex in a Hamiltonian cycle has 2) Every vertex in a Hamiltonian cycle has a degree of 2 (some edges may not be used)a degree of 2 (some edges may not be used)..

Page 39: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 39

3.3. 1) Proving there is no H.C (easy)

• The graph has 5 verticies and 6 edges. v4 and v2 have degree 3.

• Start creating the cycle without using the edges (v4, v1) and (v2,v3).

v1

v2

v3

v4v5

continued

Page 40: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 40

• But now there are only 4 edges -- not enough for a H.C involving 5 verticies.

v1

v2

v3

v4v5

Page 41: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 41

2) Proving there is no H.C (hard)

a b c

k

m

g h j

l

de

f

i

continued

Page 42: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 42

• 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

Page 43: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 43

• 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.

Page 44: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 44

3.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.

Page 45: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 45

Example

• 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.

a b

cd

3

2

3

3

11

11

Page 46: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 46

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.

Page 47: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 47

4. 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

Page 48: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 48

• For that reason, algorithms designed for real-world data only generate near-minimum length cycles– they are less time consuming, but may not g

ive the best answer

Page 49: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 49

5. Similarity Graphs

• A very common coding requirement is to collect ‘similar’ things into groups based on their properties. e.g.:– text recognition (group characters)– weather forecasting (find clouds/rainfall)– program analysis (find similar programs)

Page 50: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 50

5.1. Grouping ‘Similar’ Programs

• Similar student programs are often collected by teachers looking for students who have worked together illegally.

• In our example, we examine the properties:– no. of lines in the program– no. of return statements– no. of function calls

Page 51: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 51

5.2. Example

• Program No. lines No. returnsNo. calls 1 66 20 1 2 41 10 2 3 68 5 8 4 90 34 5 5 75 12 14

Page 52: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 52

• A vertex v is made from three properties, and so can be viewed as a kind of 3D coordinate (p1,p2,p3).

p1

p2

p3

v (10,15,30)

Page 53: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 53

• Define a dissimilarity function s() between two verticies v = (v1, v2, v3) and w = (w1, w2, w3):

s(v, w) = |v1-w1| + |v2-w2| + |v3-w3|

• v and w are similar if s(v,w) is close to 0.– because v and w are closer together in space

5.3. Dissimilarity Function

Page 54: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 54

• Using v1, v2, v3, v4, v5 for the five programs from the table:

v1 = (66, 20, 1)

v2 = (41, 10, 2)

v3 = (68, 5, 8)

v4 = (90, 34, 5)

v5 = (75, 12, 14)

Page 55: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 55

• Dissimilarity values:s(v1,v2) = 36 s(v1,v3) = 24s(v1,v4) = 42 s(v1,v5) = 30s(v2,v3) = 38 s(v2,v4) = 76s(v2,v5) = 48 s(v3,v4) = 54s(v3,v5) = 20 s(v4,v5) = 46

Page 56: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 56

• We place an edge between two verticies v and w only if s(v,w) < S

• We decide on S -- the smaller it is, the ‘closer’ v and w have to be.

Page 57: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 57

• Let s(v,w) < 25 for v1, v2, ...v5

• The resulting edges collect the programs into 3 groups:

– {1,3,5}, {2}, and {4}

• Conclusion: the students who wrote programs 1, 3, and 5 may have worked together.

– and was student no.3 the boss?

v1

v3

v5

v4

v2

5.4. The Similarity Graph

Page 58: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 58

6. Implementing Graphs

• Adjacency matricies– singular: matrix

• Adjacency lists

Page 59: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 59

6.1. An Adjacency Matrix

a b

c

ed

Graph

0 1 0 0 1

a b c d e

1 0 1 0 1

0 1 1 0 1

0 0 0 0 1

1 1 1 1 0

a

bc

d

e

Adjacency Matrix

Page 60: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 60

Properties

• The degree of a vertex v (of a simple graph) = sum of row v or sum of column v– e.g. a has degree 2 since it is connected to b an

d e

• An adjacency matrix can represent loops– e.g. vertex c on the previous slide

continued

Page 61: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 61

• An adjacency matrix cannot represent parallel edges, but that is possible if nonnegative integers are allowed as matrix entries– ijth entry = no. of edges between vertex i and j

• Not an efficient data structure since information is duplicated around the main diagonal– most data appears twice

Page 62: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 62

The Number of Paths

• If an adjacency matrix A is multiplied by itself repeatedly:– A, A2, A3, ..., An

Then the ijth entry in matrix An is equal to the number of paths from i to j of length n.

Page 63: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 63

Example

a b

c

ed

0 1 0 1 0

a b c d e

1 0 1 0 1

0 1 0 1 1

1 0 1 0 0

0 1 1 0 0

a

bc

d

e

A =

Page 64: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 64

0 1 0 1 0

1 0 1 0 1

0 1 0 1 1

1 0 1 0 0

0 1 1 0 0

A2 =

0 1 0 1 0

1 0 1 0 1

0 1 0 1 1

1 0 1 0 0

0 1 1 0 0

=

2 0 2 0 1

a b c d e

0 3 1 2 1

2 1 3 0 1

0 2 0 2 1

1 1 1 1 2

a

bc

d

e

Page 65: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 65

• Consider row a, column c in A2:

( 0 1 0 1 0 )a

b d

c

0

1

0

1

1

b

d

= 0*0 + 1*1 + 0*0 + 1*1 + 0*1= 2

Why it Works...

continued

a-b-ca-d-c

Page 66: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 66

• A non-zero product means there is at least one vertex connecting a and c.

• The sum is 2 because of:– (a, b, c) and (a, d, c)– 2 paths of length two

Page 67: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 67

The Degree of Verticies

• The entries on the main diagonal of A2 give the degrees of the verticies (when A is a simple graph).

• Consider vertex c:– degree of c = 3 since it is connected to the edge

s (c,b), (c,d), and (c,e).

continued

Page 68: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 68

• In A2 these become paths of length 2:– (c,b,c), (c,d,c), and (c,e,c)

• So the number of paths of length 2 for c = the degree of c– this is true for all verticies

Page 69: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 69

Coding Adjacency Matricies

• #define NUMNODES nint arcs[NUMNODES][NUMNODES];

• arcs[u][v] == 1 if there is an edge (u,v); 0 otherwise

• The implementation may also need a way to map node names (strings) to array indicies.

continued

Page 70: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 70

• If n is large then the array will be very large, with almost half of it being unnecessary.

• If the nodes are lightly connected then most of the array will contain 0’s, which is a further waste of memory.

Page 71: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 71

Representing Directed Graphs

• A directed graph:

0 1

32

4

Page 72: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 72

Its Adjacency Matrix

• Not symmetric; all the array may be necessary.

• Still a waste of space if nodes are lightly connected.

1 1 1 0 00 0 0 1 0

1 1 0 0 1

0 0 1 0 1

0 1 0 0 0

0

0 1 2 3 4

12

3

4

finish

star

t

Page 73: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 73

6.2. Adjacency Lists• Usually used for representing a directed gra

ph. So, for the last example:

0 1 2

3

0 1 4

2 4

1

0

1

2

3

4

successors[]

meansNULL

size of array= no. ofnodes (n) no. of cells

== no. of edges (a)

Page 74: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 74

Data Structures

• struct cell { /* for a linked list */ Node nodeName; struct cell *next;};

struct cell *successors[NUMNODES];

• successors[u] points to a linked list of cells which give the names of the nodes connected to u.

Page 75: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 75

6.3. Matricies or Lists?• Which representation is better for graphs?

• The answer varies, depending on the operations that will be applied to the graph.

• We will consider three operations:– is there an edge between u and v?– find the successors of u (in a directed graph)– find the predecessors of u (in a directed graph)

continued

Page 76: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 76

• The answer also depends on the number of edges (a) between the n nodes– a sparse graph has few edges (a << n)– a dense graph has many edges (a >> n)

continued

Page 77: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 77

• In the most dense graph, a graph of n nodes will have n(n-1)/2 edges.

• In that case, for large n, a = O(n2)

nodes = 5edges = (5*4)/2 = 10

Page 78: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 78

• Proof that a graph of n nodes has n(n-1)/2 edges. Write as S(n) = n(n-1)/2

• Basis. S(2) = 1. True.• Inductive Case.

– assume S(n) = n(n-1)/2 (1)– try to show S(n+1) = (n+1)n/2 (2)

– we know: S(n+1) = S(n) + n which is– S(n+1) = n(n-1)/2 + n which is– S(n+1) = (n+1)n/2 which is (2)

Page 79: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 79

6.3.1. Is there an edge (u,v)?

• Adjacency matrix: O(1) to read arcs[u][v]

• Adjacency list: O(1 + a/n)– O(1) to get to successors[u]– length of linked list is on average a/n

– if a sparse graph (a<<n): O(1+ a/n) => O(1)– if a dense graph (a~=n2): O(1+ a/n) => O(n)

Page 80: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 80

6.3.2. Find u’s successors (u->v)

• Adjacency matrix: O(n) since must examine the entire row for vertex u

• Adjacency list: O(1+(a/n)) since must look at entire list pointed to by successors[u]– if a sparse graph (a<<n): O(1+a/n) => O(1)– if a dense graph (a~=n2): O(1+a/n) => O(n)

Page 81: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 81

6.3.3. Find u’s predecessors (t->u)

• Adjacency matrix: O(n) since must examine the entire column for vertex u– a 1 in the row for ‘t’ means that ‘t’ is a predecessor

• Adjacency list: O(a) since must examine every list pointed to by successors[]– if a sparse graph (a<<n): O(a) is fast– if a dense graph (a~=n2): O(a) is slow

Page 82: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 82

6.3.4. Summary: which is faster?

• Operation Dense Graph Sparse GraphFind edge Adj. Matrix EitherFind succ. Either Adj. listFind pred. Adj. Matrix Either

• As a graph gets denser, an adjacency matrix has better execution time than an adjacency list.

Page 83: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 83

6.5. Why bother with an Adj. List?

• An adjacency list might use less storage space than an adjacency matrix for the same graph.

• The size of an adjacency matrix for a graph of n nodes is:– n2 bits (assuming 0 and 1 are stored as bits)

continued

Page 84: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 84

• An adjacency list cell uses:– 32 bits for the integer, 32 bits for the pointer– so, cell size = 64 bits

• Total no. of cells = total no. of edges, a– so, total size of lists = 64a bits

• successors[] has n entries (for n nodes)– so, array size is 32n bits

• Total size of an adjacency list data struct:64a + 32n

Page 85: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 85

Size Comparison

• An adjacency list will use less storage than an adjacency matrix when:

64a + 32n < n2

– which is: a < n2/64 - n/2

• When n is large, ignore the n/2 term:a < n2/64

continued

Page 86: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 86

• n2 is (roughly) the maximum number of edges.

• So if the actual number of edges in a graph is 1/64 of the maximum number of edges, then an adj. list representation will be smaller than an adj. matrix coding– but the graph must be quite sparse

Page 87: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 87

7. 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.

• Problem: cycles• Solution: DFS ‘marks’ nodes as it visits them, and n

ever revisits marked nodes.

Page 88: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 88

• DFS is “depth first” because it always fully explores down a path away from a vertex v before it looks at other paths leaving v.

Page 89: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 89

7.1. Directed Graph Example

a

d

fec

b

Graph G

Page 90: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 90

7.2. Data Structures• enum MARKTYPE {VISITED, UNVISITED};

struct cell { /* adj. list */ NODE nodeName; struct cell *next;};typedef struct cell *LIST;

struct graph { enum MARKTYPE mark; LIST successors;};typedef struct graph GRAPH[NUMNODES];

Page 91: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 91

7.3. The dfs() Functionvoid dfs(NODE u, GRAPH G)// recursively search G, starting from u{ LIST p; // runs down adj. list of u NODE v; // node in cell that p points at

G[u].mark = VISITED; // visited u p = G[u].successors; while (p != NULL) { // visit u’s succ’s v = p->nodeName; if (G[v].mark == UNVISITED) dfs(v, G); // visit v p = p->next; }}

Page 92: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 92

7.4. Calling dfs(a,G)

• Call Visitedd(a) {a}d(a)-d(b) {a,b}d(a)-d(b)-d(c) {a,b,c}

Skip b, return to d(b)d(a)-d(b)-d(d) {a,b,c,d}

Skip cd(a)-d(b)-d(d)-d(e) {a,b,c,d,e}

Skip c, return to d(d)

continued

call it d(a)for short

Page 93: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 93

d(a)-d(b)-d(d)-d(f) {a,b,c,d,e,f}Skip c, return to d(d)

d(a)-d(b)-d(d) {a,b,c,d,e,f}Return to d(b)

d(a)-d(b) {a,b,c,d,e,f}Return to d(a)

d(a) {a,b,c,d,e,f}Skip d, return

Page 94: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 94

7.5. DFS Tree

• Since nodes are marked, the graph is searched as if it were a tree:

d/4

f/6e/5c

b/2

a/1

c/3

Page 95: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 95

7.6. dfs() 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

Page 96: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 96

• If the graph is dense, a >> n, the O(n) term can be ignored– in that case, the total running time = O(a)

Page 97: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 97

7.7. Uses of DFS

• Finding cycles in a graph– e.g. for finding recursive dependencies in a calli

ng graph

• Reachability detection– i.e. can a vertex v be reached from vertex u?– useful for e-mail routing

continued

Page 98: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 98

• Finding a topological order for an acyclic graph– e.g. used to order tasks where u->v means do ta

sk u then task v

a

b

c

d

e

f

a

b/3

c/5

d/6

e/4

ftopologicalorder

/1

/2

Page 99: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 99

8. 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.

Page 100: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 100

8.1. Example Weighted Graph

a

b c

z

gf

d e

2

2

1432

473

5

61

Problem: find the shortest path from vertex a to vertex z.

Page 101: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 101

8.2. Dijkstra’s Shortest Path Algorithm

• Due to Edsger W. Dijkstra (1959, when 29).

• 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 infi

nity (Inf)

continued

Page 102: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 102

• 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!

Page 103: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 103

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

Page 104: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 104

// find shortest path to z while (z in T) { // 5 choose v from T with min S(v); // 6 T = T without v; // 7 for (each x in T adjacent to v) // 8 S(x) = smaller_of(

S(x), S(v)+w(v,x) ); // 9 } // 10 return S(z); // return shortest path}

Page 105: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 105

Edsger Wybe Dijkstra In 1997,aged 67

Page 106: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 106

8.3. Processing the Example

a

b c

z

gf

d e

2

2

1432

473

5

61

continued

Page 107: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 107

Initialisation

a

b c

z

gf

d e

2

2

1432

473

5

61

continued

Inf Inf

InfInfInf

Inf

Inf

= temporary score

0

Page 108: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 108

First Iteration

a

b c

z

gf

d e

2

2

1432

473

5

61

continued

2 Inf

InfInfInf

Inf

1

0

= changed temporary

= permanent score

Page 109: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 109

Second Iteration

a

b c

z

gf

d e

2

2

1432

473

5

61

continued

2 Inf

Inf4Inf

6

0

1

Page 110: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 110

Third Iteration

a

b c

z

gf

d e

2

2

1432

473

5

61

4

Inf46

6

0

1

2

continued

Page 111: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 111

Fourth Iteration

a

b c

z

gf

d e

2

2

1432

473

5

61

56

6

0

1

2 4

4

Could have chosen‘d’ instead.

continued

Page 112: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 112

Fifth Iteration

a

b c

z

gf

d e

2

2

1432

473

5

61

56

6

0

1

2 4

4

Choosing ‘d’ is awaste of time.

continued

Page 113: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 113

Sixth (and final) Iteration

a

b c

z

gf

d e

2

2

1432

473

5

61

6

6

0

1

2

4

4

5

Finished!Shortest pathfrom ‘a’ to‘z’ is length 5.

Page 114: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 114

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.

Page 115: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 115

8.4. Proof of the Algorithm

• Inductive statement T(i):– on the ith iteration of the loop on lines 5-10 of th

e algorithm, the score S(v) for vertex v is made permanent

– s(v) is the minimum of the temp scores

– that score, S(v), is the shortest path from vertex a to vertex v

continued

Page 116: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 116

• Basis (i = 1).– initially S(a) = 0, and all other S()’s = Inf– so vertex a will be chosen– S(a) is the shortest path from vertex a to a

– T(1) is true.

continued

Page 117: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 117

• Induction Case.– Assume that T(k) is true for all k < i.– On the k+1 iteration, select node v.

Is S(v) the shortest path from a to v?

Page 118: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 118

Diagram of Induction Case

PermanentScores Temporary

Scoresa

w

u

v Choose v.

Is s(v) the shortestpath froma to v ?

Page 119: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 119

Proof by Contradiction

• Assume that S(v) is not the shortest path from a to v.

• That means there must be another path:a -> ... -> w -> u -> ... -> v

which is shorter.

continued

something notin permanent

Page 120: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 120

• This means that:path_length(a -> ... -> w -> u -> ... -> v) <path_length(a -> ... -> v)

• which means that: path_length(a -> ... -> w -> u) <path_length(a -> ... -> v)

• which means that:S(u) < S(v)

continued

Page 121: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 121

• But, if S(u) < S(v) then u should have been selected instead of v on the k+1 th iteration.

• Since v was chosen, it means that:S(u) >= S(v), for all u

• So S(v) is the shortest path.

continued

Page 122: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 122

• The induction case was:– On the k+1 iteration, select node v.

Is S(v) the shortest path from a to v?– The answer is yes.– So, T(k+1) is true.

• Since the basis and induction cases are true, T(i) is true for all i.

Page 123: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 123

8.5. Execution Time (worst case)

• The graph has n verticies.

• T is the set of verticies with temporary scores– initially it contains all n verticies

Page 124: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 124

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

Page 125: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 125

• 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

Page 126: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 126

Is Dijkstra’s Algorithm Optimal?• Optimal = the fastest running time

(for the given assumptions).

• In the worst case, a graph with n nodes has n(n-1)/2 edges.

• Finding the shortest path for a node in the worst case requires looking at all the edges:– O( n(n-1)/2 ) = O(n2)

continued

Page 127: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 127

• Dijkstra’s algorithm has the same running time in the worst case, so it is optimal in that situation.

Page 128: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 128

9. Planar Graphs

Paris Berlin Bonn

Madrid Rome Geneva

• Can the cities be connected by roads without using bridges (or intersections)?– i.e. the roads should not cross

Is this graph planar?

Page 129: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 129

9.1. Definition

• A graph is planar if it can be drawn in the plane (e.g. on paper, on a computer screen) without its edges crossing.

Page 130: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 130

9.2. A Connected Planar Graph

1 2

34

56B

C

AD

continued

Page 131: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 131

• A connected planar graph divides the plane (the paper) into distinct regions called faces.

• A face is defined by a cycle in the graph’s nodes.

continued

Page 132: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 132

• The example has 4 faces: A, B, C, and D– Face Cycle

A (5,2,3,4,5)B (1,5,4,6,1)C (1,2,5,1)D (1,2,3,4,6,1)

• D is the outer face.

Page 133: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 133

9.3. Euler’s Formula

• Euler (1752) proved that for any connected graph, where:

f = no. of facese = no. of edgesv = no. of verticies/nodes

then the formula holds:

f = e - v + 2

Page 134: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 134

9.4. Showing a Graph is not Planar

• Consider the cities example:

Paris Berlin Bonn

Madrid Rome Geneva

continued

Assume there are f faces, e edges.Assume there are f faces, e edges.

Page 135: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 135

• Each cycle uses at least 4 edges, so a face is bounded by at least 4 lines.– total no. of lines used for all faces >= 4f (1)

• A line is an edge used at most twice, so:– total no. of lines for all faces <= 2e (2)

• Combining (1) and (2):2e >= 4f (3)

continued

Page 136: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 136

• Replace the f value in (3) with Euler’s formula (assuming the graph is planar):

2e >= 4(e - v + 2)

• From the graph, we see that e = 9 and v = 6. So:2*9 >= 4*(9 - 6 + 2)

18 >= 20, which is a contradiction

• So the graph is not planar.

Page 137: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 137

9.5. Uses of Planarity

• For drawing nice graphics– display diagrams without crossing lines

• Integrated circuit (IC) design:– ICs can have several layers, but on a given laye

r the circuit cannot have components that cross over each other

Page 138: 241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms

241-303 Discrete Maths: Graphs/5 138

10. Further Information

• DM: Chapter 6 “Graph Theory”