CS1022 Computer Programming & Principles

Slides:



Advertisements
Similar presentations
Chapter 28 Weighted Graphs and Applications
Advertisements

Chapter 11 Trees Graphs III (Trees, MSTs) Reading: Epp Chp 11.5, 11.6.
Mathematical Preliminaries
Unit-iv.
Analysis of Algorithms
Introduction to Algorithms 6.046J/18.401J/SMA5503
Polygon Scan Conversion – 11b
Reductions Complexity ©D.Moshkovitz.
CS16: Introduction to Data Structures & Algorithms
Chapter 4: Informed Heuristic Search
Chapter Objectives To learn about recursive data structures and recursive methods for a LinkedList class To understand how to use recursion to solve the.
Chapter 23 Minimum Spanning Tree
COMP 482: Design and Analysis of Algorithms
Great Theoretical Ideas in Computer Science
CS1022 Computer Programming & Principles
CS1022 Computer Programming & Principles Lecture 8.1 Digraphs (1)
CS1022 Computer Programming & Principles Lecture 7.1 Graphs (1)
CS1022 Computer Programming & Principles
25 seconds left…...
CS203 Lecture 15.
Foundations of Data Structures Practical Session #7 AVL Trees 2.
© 2008 Pearson Addison-Wesley. All rights reserved Chapter 13 Mathematics and Business.
Bart Jansen 1.  Problem definition  Instance: Connected graph G, positive integer k  Question: Is there a spanning tree for G with at least k leaves?
Section 3.4 The Traveling Salesperson Problem Tucker Applied Combinatorics By Aaron Desrochers and Ben Epstein.
Introduction to Graph Theory Instructor: Dr. Chaudhary Department of Computer Science Millersville University Reading Assignment Chapter 1.
Great Theoretical Ideas in Computer Science for Some.
22C:19 Discrete Structures Trees Spring 2014 Sukumar Ghosh.
Lecture 21 Paths and Circuits CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.
Chapter 15 Graph Theory © 2008 Pearson Addison-Wesley. All rights reserved.
1 Chapter 15.3 Hamilton Paths and Hamilton Circuits Objectives 1.Understand the definitions of Hamilton paths & Hamilton circuits. 2.Find the number of.
CMPS 2433 Discrete Structures Chapter 5 - Trees R. HALVERSON – MIDWESTERN STATE UNIVERSITY.
Great Theoretical Ideas in Computer Science.
1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320.
3 -1 Chapter 3 The Greedy Method 3 -2 The greedy method Suppose that a problem can be solved by a sequence of decisions. The greedy method has that each.
CS2420: Lecture 40 Vladimir Kulyukin Computer Science Department Utah State University.
Discrete Math for CS Chapter 7: Graphs. Discrete Math for CS Map of Koenigsberg at the time of Euler.
Design and Analysis of Algorithms Minimum Spanning trees
Module #1 - Logic 1 Based on Rosen, Discrete Mathematics & Its Applications. Prepared by (c) , Michael P. Frank and Modified By Mingwu Chen Trees.
Chapter 15 Graph Theory © 2008 Pearson Addison-Wesley. All rights reserved.
May 5, 2015Applied Discrete Mathematics Week 13: Boolean Algebra 1 Dijkstra’s Algorithm procedure Dijkstra(G: weighted connected simple graph with vertices.
© The McGraw-Hill Companies, Inc., Chapter 3 The Greedy Method.
Programming for Geographical Information Analysis: Advanced Skills Online mini-lecture: Introduction to Networks Dr Andy Evans.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Slide 14-1 Copyright © 2005 Pearson Education, Inc. SEVENTH EDITION and EXPANDED SEVENTH EDITION.
Graph Theory Hamilton Paths and Hamilton Circuits.
COSC 2007 Data Structures II Chapter 14 Graphs III.
May 1, 2002Applied Discrete Mathematics Week 13: Graphs and Trees 1News CSEMS Scholarships for CS and Math students (US citizens only) $3,125 per year.
Representing and Using Graphs
Foundations of Discrete Mathematics
Spring 2015 Mathematics in Management Science Network Problems Networks & Trees Minimum Networks Spanning Trees Minimum Spanning Trees.
CSCI 115 Chapter 7 Trees. CSCI 115 §7.1 Trees §7.1 – Trees TREE –Let T be a relation on a set A. T is a tree if there exists a vertex v 0 in A s.t. there.
Agenda Review: –Planar Graphs Lecture Content:  Concepts of Trees  Spanning Trees  Binary Trees Exercise.
Spring 2015 Mathematics in Management Science Network Problems Networks & Trees Minimum Networks Spanning Trees Minimum Spanning Trees.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path.
Chapter 14 Section 3 - Slide 1 Copyright © 2009 Pearson Education, Inc. AND.
Spanning Trees Dijkstra (Unit 10) SOL: DM.2 Classwork worksheet Homework (day 70) Worksheet Quiz next block.
Grade 11 AP Mathematics Graph Theory Definition: A graph, G, is a set of vertices v(G) = {v 1, v 2, v 3, …, v n } and edges e(G) = {v i v j where 1 ≤ i,
CSE 589 Applied Algorithms Spring 1999 Prim’s Algorithm for MST Load Balance Spanning Tree Hamiltonian Path.
Graphs. What is a graph? In simple words, A graph is a set of vertices and edges which connect them. A node (or vertex) is a discrete position in the.
MAT 110 Workshop Created by Michael Brown, Haden McDonald & Myra Bentley for use by the Center for Academic Support.
Graph Search Applications, Minimum Spanning Tree
Hamilton Paths and Hamilton Circuits
D1 Discrete Mathematics
CS1022 Computer Programming & Principles
Redraw these graphs so that none of the line intersect except at the vertices B C D E F G H.
Great Theoretical Ideas in Computer Science
Discrete Mathematics for Computer Science
D1 Discrete Mathematics
Graphs Chapter 13.
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. 2002. (Chapter 7) Wikipedia’s entry on graph theory Wikibooks entry on graph theory CS1022