Download presentation

Presentation is loading. Please wait.

Published byMarquez Ness Modified over 2 years ago

1
CS1022 Computer Programming & Principles Lecture 7.2 Graphs (2)

2
Plan of lecture Hamiltonian graphs Trees Sorting and searching 2 CS1022

3
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 3 CS1022 W. R. Hamilton

4
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 K n 4 CS1022

5
Hamiltonian graphs (3) Example: complete graph K 5 – 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 2 nd vertex, then – We have 3 options to move to the 3 rd vertex, then – We have 2 options to move to the 4 th vertex, then – We have 1 option to move to the 5 th vertex – That is, 4 3 2 1 4! 24 cycles 5 CS1022 a b e c d

6
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 6 CS1022

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

8
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) 8 CS1022

9
Nearest neighbour (sub-optimal) algorithm Travelling salesperson problem (3) 9 CS1022 begin choose v V; route := v; w := 0; v:= v; % initialise variables mark v; while unmarked vertices remain do begin 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 far v:= u; % update current vertex mark v; % mark current vertex end route := route v; % append origin to close cycle w := w (weight of edge vv); output (route, w) end

10
Trace nearest neighbour (sub-optimal) algorithm Travelling salesperson problem (4) 10 CS1022 begin choose v V; route := v; w := 0; v:= v; mark v; while unmarked vertices remain do begin choose an unmarked vertex u closest to v; route := route u; w := w (weight of edge vu); v:= u; mark v; end route := route v; w := w (weight of edge vv); output (route, w) end AB DC uroutew v Initially–D0D uroutew v Initially–D0D CDC3C uroutew v Initially–D0D CDC3C ADCA9A uroutew v Initially–D0D CDC3C ADCA9A BDCAB14B uroutew v Initially–D0D CDC3C ADCA9A BDCAB14B Exit loopBDCABD24B

11
“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 Hamiltonian cycles – Enumerating all would take too much time and memory Travelling salesperson problem (5) 11 CS1022 AB DC

12
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) 12 CS1022 A B D C E F

13
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) 13 CS1022

14
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 G 2.Add further edges of G without creating cycles 3.Do 2 until no more edges can be added (w/o creating cycle) Spanning trees (1) 14 CS1022

15
Find two spanning trees for the graph Spanning trees (2) 15 CS1022 ab e c d f g ba d f b e c g Solution 1 Solution 2

16
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 16 CS1022

17
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) 17 CS1022 begin e := an edge of E of smallest weight; T := e ; E:= E – e ; while E do begin 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; end output T; end

18
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) 18 CS1022 A B D C E F

19
Rooted trees defined recursively: A single vertex is a tree (with that vertex as root) If T 1, T 2, , T k are disjoint trees with roots v 1, v 2, , v k we can “attach” a new vertex v to each v i to form a new tree T with root v Rooted trees (2) 19 CS T1T1 v1v1 T2T2 v2v2 TkTk vkvk v Each vertex in a rooted tree T forms the root of another rooted tree which we call a subtree of T

20
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) 20 CS1022

21
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 21 CS1022 Left Right v

22
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) 22 CS1022 < Left > Right v

23
Example of binary search tree with words MY COMPUTER HAS A CHIP ON ITS SHOULDER Sorting and searching (2) 23 CS1022 MY COMPUTERON AHAS CHIP SHOULDER ITS

24
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) 24 CS1022

25
Algorithm to find (or not) item in binary tree Binary search (1) 25 CS1022 search(x, tree) begin if tree null then return false else Let tree be of form (left_subtree, root, right_subtree) if x root then return true else if x root then return search(x, left_subtree) else return search(x, right_subtree) end

26
Algorithm to find (or not) item in binary tree Binary search (2) 26 CS1022 search(x, tree) begin if tree null then return false else Let tree be of form (left_subtree, root, right_subtree) if x root then return true else if x root then return search(x, left_subtree) else return search(x, right_subtree) End K CT VM K CT VM search(R, ) left_sub= root= right_sub = C K T VM search(R, ) T VM left_sub= root= right_sub = M T V search(R, ) V left_sub= null root= right_sub = null V search(R, null) false

27
Further reading R. Haggarty. “Discrete Mathematics for Computing”. Pearson Education Ltd (Chapter 7) Wikipedia’s entry on graph theory Wikibooks entry on graph theory 27 CS1022

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google