1 / 27

CS1022 Computer Programming & Principles

CS1022 Computer Programming & Principles. Lecture 7.2 Graphs (2). 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:

kineta
Download Presentation

CS1022 Computer Programming & Principles

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS1022Computer Programming & Principles Lecture 7.2 Graphs (2)

  2. Plan of lecture • Hamiltonian graphs • Trees • Sorting and searching 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 W. R. Hamilton CS1022

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

  5. 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 b c a e d CS1022

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

  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 CS1022

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

  9. Travelling salesperson problem (3) • Nearest neighbour (sub-optimal) algorithm CS1022

  10. Travelling salesperson problem (4) • Trace nearest neighbour (sub-optimal) algorithm 5 8 7 6 10 3 B D A C CS1022

  11. 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 5 8 7 6 10 3 B D A C CS1022

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

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

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

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

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

  17. Minimal spanning tree algorithm (1) • G (V, E) is a connected graph with weighted edges • Algorithm finds MST for Gby successively selecting edges of least possible weight to build an MST • MST is stored as a set T of edges CS1022

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

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

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

  21. 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 rightsubtrees of the vertex • A missing subtree is called a null tree v Left Right CS1022

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

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

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

  25. Binary search (1) • Algorithm to find (or not) item in binary tree CS1022

  26. Binary search (2) • Algorithm to find (or not) item in binary tree K K C M C C T T T T V M M M M V V V V V search(R, ) left_sub= root= right_sub = K search(R, ) left_sub= root= right_sub = T search(R, ) left_sub= null root= right_sub = null V search(R, null) false CS1022

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

More Related