CS1022 Computer Programming & Principles

27
CS1022 Computer Programming & Principles Lecture 2 Graphs

description

CS1022 Computer Programming & Principles. Lecture 2 Graphs. Plan of lecture. Hamiltonian graphs Trees Sorting and searching. Hamiltonian graphs (1). Euler looked into the problem of using all edges once (visiting vertices as many times as needed) Interesting related problem: - PowerPoint PPT Presentation

Transcript of CS1022 Computer Programming & Principles

Page 1: CS1022 Computer Programming & Principles

CS1022 Computer Programming &

Principles

Lecture 2Graphs

Page 2: CS1022 Computer Programming & Principles

Plan of lecture• Hamiltonian graphs• Trees• Sorting and searching

2CS1022

Page 3: CS1022 Computer Programming & Principles

Hamiltonian graphs (1)• Euler looked into the problem of using all edges

once (visiting vertices as many times as needed) • Interesting related problem: – Find a cycle which passes through all vertices once

• A Hamiltonian cycle includes all vertices in a graph• Hamiltonian graphs have Hamiltonian cycles– Useful for planning train timetables– Useful for studying telecommunications

3CS1022 W. R. Hamilton

Page 4: CS1022 Computer Programming & Principles

Hamiltonian graphs (2)• Unlike the Eulerian problem, there is no simple rule

for detecting Hamiltonian cycles– One of the major unsolved problems in graph theory

• Many graphs are Hamiltonian– If each vertex is adjacent (has an edge) to every other

vertex, then there is always a Hamiltonian cycle– These are called complete graphs– A complete graph with n vertices is denoted Kn

4CS1022

Page 5: CS1022 Computer Programming & Principles

Hamiltonian graphs (3)• Example: complete graph K5

– A Hamiltonian cycle is a b c d e a– There are several others

• Since each vertex is adjacent to every other vertex– We have 4 options to move to the 2nd vertex, then– We have 3 options to move to the 3rd vertex, then– We have 2 options to move to the 4th vertex, then– We have 1 option to move to the 5th vertex– That is, 4 3 2 1 4! 24 cycles

5CS1022

a

b

e

c

d

Page 6: CS1022 Computer Programming & Principles

Hamiltonian graphs (4)• Finding Hamiltonian cycles in an arbitrary graph is

not straightforward• Deciding if a graph is Hamiltonian is can be quite

demanding• Problem:

1. Input a graph G (V, E)2. Analyse G 3. If it is Hamiltonian output YES, otherwise output NO

• The test “if it is Hamiltonian” is not straightforward

6CS1022

Page 7: CS1022 Computer Programming & Principles

Travelling salesperson problem (1)• Hamiltonian graphs model many practical problems• Classic problem: travelling salesperson– A salesperson wishes to visit a number of towns

connected by roads– Find a route visiting each town exactly once, and keeping

travelling costs to a minimum• The graph modelling the problem is Hamiltonian– Vertices are town and edges are roads– Additionally, edges have a weight to represent cost of

travel along road (e.g., petrol, time/distance it takes)• Search a Hamiltonian cycle of minimal total weight

7CS1022

Page 8: CS1022 Computer Programming & Principles

• No efficient algorithm to solve problem– Complex graphs have too many Hamiltonian cycles– They all have to be considered, in order to find the one

with minimal total weight• There are algorithms for sub-optimal solutions– Sub-optimal: not minimal, but considerably better than

an arbitrary choice of Hamiltonian cycle

Travelling salesperson problem (2)

8CS1022

Page 9: CS1022 Computer Programming & Principles

• Nearest neighbour (sub-optimal) algorithmTravelling salesperson problem (3)

9CS1022

beginchoose v V;route := v; w := 0; v:= v; % initialise variablesmark v;while unmarked vertices remain dobegin

choose an unmarked vertex u closest to v;route := route u; % append u to end of

route w := w (weight of edge vu); % update weight of route so farv:= u; % update current

vertexmark v; % mark current

vertexendroute := route v; % append origin to close cyclew := w (weight of edge vv);output (route, w)

end

Page 10: CS1022 Computer Programming & Principles

• Trace nearest neighbour (sub-optimal) algorithmTravelling salesperson problem (4)

10CS1022

beginchoose v V;route := v; w := 0; v:= v; mark v;while unmarked vertices remain dobegin

choose an unmarked vertex u closest to v;route := route u; w := w (weight of edge vu);v:= u; mark v;

endroute := route v; w := w (weight of edge vv);output (route, w)

end

A B

DC

5

78

3

6 10

u route w vInitially – D 0 D

u route w vInitially – D 0 D

C DC 3 C

u route w vInitially – D 0 D

C DC 3 CA DCA 9 A

u route w vInitially – D 0 D

C DC 3 CA DCA 9 AB DCAB 14 B

u route w vInitially – D 0 D

C DC 3 CA DCA 9 AB DCAB 14 B

Exit loop B DCABD 24 B

Page 11: CS1022 Computer Programming & Principles

• “Nearest” “with lower weight on edge”• Exhaustive search finds 2 other solutions:– ABCDA (total weight 23)– ACBDA (total weight 31)

• It is not the best solution, but it’s better than 31• A complete graph with 20 vertices has 6 1016

Hamiltonian cycles– Enumerating all would take too

much time and memory

Travelling salesperson problem (5)

11CS1022

A B

DC

5

78

3

6 10

Page 12: CS1022 Computer Programming & Principles

• Special type/class of graphs called trees– Very popular in computing applications/solutions

• A tree is a connected and acyclic graph G (V, E)• In the literature, trees are drawn upside down

Trees (1)

12CS1022

AB

D

C

EF

Page 13: CS1022 Computer Programming & Principles

• Let G (V, E) be a tree, |V| n, |E| m• We can state (all are equivalent)– There is exactly one path between any vertices of G – G is connected and m n – 1 – G is connected and the removal of one single edge

disconnects it– G is acyclic and adding a new edge creates a cycle

Trees (2)

13CS1022

Page 14: CS1022 Computer Programming & Principles

• Any connected graph G contains trees as sub-graphs• A sub-graph of G which is a tree and includes all

vertices is a spanning tree• It is straightforward to build a spanning tree:

1. Select an edge of G2. Add further edges of G without creating cycles3. Do 2 until no more edges can be added (w/o creating

cycle)

Spanning trees (1)

14CS1022

Page 15: CS1022 Computer Programming & Principles

• Find two spanning trees for the graphSpanning trees (2)

15CS1022

a b

ec

d fg

ba

d f

b

ec

g

• Solution 1 • Solution 2

Page 16: CS1022 Computer Programming & Principles

• Process adapted for minimum connector problem:– A railway network connecting many towns is to be built– Given the costs of linking 2 towns, find a network of

minimal total cost• Spanning tree for a graph with weighted edges, with

minimal total weight– This is called minimal spanning tree (MST)

• Unlike the travelling salesperson, we have efficient algorithms to solve this problem– We can find the optimal solution!

Minimal spanning tree

16CS1022

Page 17: CS1022 Computer Programming & Principles

• G (V, E) is a connected graph with weighted edges• Algorithm finds MST for G by successively selecting

edges of least possible weight to build an MST– MST is stored as a set T of edges

Minimal spanning tree algorithm (1)

17CS1022

begine := an edge of E of smallest weight;T := e;E:= E –e;while E dobegin

e := an edge of E of smallest weight;T := T e; E:= set of edges in (E – T) which do not create cycles if added to T;

endoutput T;

end

Page 18: CS1022 Computer Programming & Principles

• We often need to represent information which is naturally hierarchical– Example: family trees

• We make use of rooted trees– A special vertex is called the root of the tree

• The root of the tree has unique features– Oldest, youngest, smallest, highest, etc.

Rooted trees (1)

18CS1022

AB

D

C

EF

Page 19: CS1022 Computer Programming & Principles

Rooted trees defined recursively:• A single vertex is a tree (with that vertex as root)• If T1, T2, , Tk are disjoint trees with roots v1, v2, , vk

we can “attach” a new vertex v to each vi to form a new tree T with root v

Rooted trees (2)

19CS1022

...T1

v1

T2

v2

Tk

vk

v Each vertex in a rooted tree T forms the root of another rooted tree which we call a subtree of T

Page 20: CS1022 Computer Programming & Principles

• Top vertex is the root and vertices at bottom of tree (those with no children) are called leaves

• Vertices other than root or leaves are called internal vertices

Rooted trees (3)

20CS1022

Page 21: CS1022 Computer Programming & Principles

• Rooted trees used as models in many areas– Computer science, biology, management

• Very important in computing: binary rooted trees– Each vertex has at most two children– Subtrees: left- and right subtrees of the vertex– A missing subtree is called a null tree

Binary (rooted) trees

21CS1022Left Right

v

Page 22: CS1022 Computer Programming & Principles

• Binary rooted trees are useful to support decisions, especially those requiring sorting/searching data– Ordered numbers, strings ordered lexicographically

• Ordered data stored as vertices of binary tree– Data in left-subtree less than data item stored in v– Data in right-subtree greater than data item stored in v

• These are called binary search trees

Sorting and searching (1)

22CS1022

<Left

>Right

v

Page 23: CS1022 Computer Programming & Principles

• Example of binary search tree with wordsMY COMPUTER HAS A CHIP ON ITS SHOULDER

Sorting and searching (2)

23CS1022

MY

COMPUTER ON

A HAS

CHIP

SHOULDER

ITS

Page 24: CS1022 Computer Programming & Principles

• Binary search trees allow efficient algorithms for– Searching for data items– Inserting new data items– Printing all data in an ordered fashion

Sorting and searching (3)

24CS1022

Page 25: CS1022 Computer Programming & Principles

• Algorithm to find (or not) item in binary treeBinary search (1)

25CS1022

search(x, tree)begin

if tree null thenreturn false

elseLet tree be of form (left_subtree, root, right_subtree)if x root then

return trueelse

if x root thenreturn search(x, left_subtree)

elsereturn search(x, right_subtree)

end

Page 26: CS1022 Computer Programming & Principles

• Algorithm to find (or not) item in binary treeBinary search (2)

26CS1022

search(x, tree)begin

if tree null thenreturn false

elseLet tree be of form (left_subtree, root, right_subtree)if x root then

return trueelse

if x root thenreturn search(x, left_subtree)

elsereturn search(x, right_subtree)

End

KC T

VMK

C T

VMsearch(R, ) left_sub= root= right_sub =C K T

VM

search(R, )T

VMleft_sub= root= right_sub =M T V

search(R, )V left_sub= null root= right_sub = nullV

search(R, null) false

Page 27: CS1022 Computer Programming & Principles

Further reading• R. Haggarty. “Discrete Mathematics for

Computing”. Pearson Education Ltd. 2002. (Chapter 7)

• Wikipedia’s entry on graph theory• Wikibooks entry on graph theory

27CS1022