# CS1022 Computer Programming & Principles

## Presentation on theme: "CS1022 Computer Programming & Principles"— Presentation transcript:

CS1022 Computer Programming & Principles
Lecture 7.2 Graphs (2)

Plan of lecture Hamiltonian graphs Trees Sorting and searching CS1022

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

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 CS1022

Hamiltonian graphs (3) a b e c d 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 CS1022

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: Input a graph G  (V, E) Analyse G If it is Hamiltonian output YES, otherwise output NO The test “if it is Hamiltonian” is not straightforward CS1022

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 CS1022

Travelling salesperson problem (2)
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 CS1022

Travelling salesperson problem (3)
Nearest neighbour (sub-optimal) algorithm begin choose v  V; route := v; w := 0; v:= v; % initialise variables mark v; while unmarked vertices remain do choose an unmarked vertex u closest to v; route := route u; % append u to end of route w := w  (weight of edge vu); % 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 vv); output (route, w) CS1022

Travelling salesperson problem (4)
Trace nearest neighbour (sub-optimal) algorithm begin choose v  V; route := v; w := 0; v:= v; mark v; while unmarked vertices remain do choose an unmarked vertex u closest to v; route := route u; w := w  (weight of edge vu); v:= u; end route := route v; w := w  (weight of edge vv); output (route, w) A B D C 5 7 8 3 6 10 u route w v Initially D C DC 3 A DCA 9 B DCAB 14 u route w v Initially D C DC 3 A DCA 9 B DCAB 14 Exit loop DCABD 24 u route w v Initially D C DC 3 A DCA 9 u route w v Initially D C DC 3 u route w v Initially D CS1022

Travelling salesperson problem (5)
“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 A B D C 5 7 8 3 6 10 CS1022

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

Trees (2) 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 CS1022

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

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

Minimal spanning tree 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! CS1022

Minimal spanning tree algorithm (1)
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 begin e := an edge of E of smallest weight; T := e; E:= E –e; while E   do 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; CS1022

Rooted trees (1) 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. A B D C E F CS1022

... Rooted trees (2) 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 v Each vertex in a rooted tree T forms the root of another rooted tree which we call a subtree of T T1 v1 T2 v2 Tk vk ... CS1022

Rooted trees (3) 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 CS1022

Binary (rooted) trees 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 v Left Right CS1022

Sorting and searching (1)
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 v < Left > Right CS1022

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

Sorting and searching (3)
Binary search trees allow efficient algorithms for Searching for data items Inserting new data items Printing all data in an ordered fashion CS1022

Binary search (1) Algorithm to find (or not) item in binary tree
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 if x  root then return search(x, left_subtree) return search(x, right_subtree) end CS1022

Binary search (2) Algorithm to find (or not) item in binary tree K C T
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 if x  root then return search(x, left_subtree) return search(x, right_subtree) End K C T V M K C T V M search(R, ) left_sub= root= right_sub = C K T V M search(R, ) T V M left_sub= root= right_sub = M T V search(R, ) V left_sub= null root= right_sub = null V search(R, null) false CS1022

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