Discrete Maths 11. Graph Theory Objective

Slides:



Advertisements
Similar presentations
CSE 211 Discrete Mathematics
Advertisements

22C:19 Discrete Math Graphs Fall 2010 Sukumar Ghosh.
22C:19 Discrete Math Graphs Fall 2014 Sukumar Ghosh.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
CSE 373 Graphs 1: Concepts, Depth/Breadth-First Search
Edited by Malak Abdullah Jordan University of Science and Technology Data Structures Using C++ 2E Chapter 12 Graphs.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Applied Discrete Mathematics Week 12: Trees
Graph & BFS.
© 2004 Goodrich, Tamassia Graphs1 ORD DFW SFO LAX
Graph COMP171 Fall Graph / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D E A C F B Vertex Edge.
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
1 Graphs ORD DFW SFO LAX Many slides taken from Goodrich, Tamassia 2004.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
Graphs1 Part-H1 Graphs ORD DFW SFO LAX
Graphs1 ORD DFW SFO LAX Graphs2 Outline and Reading Graphs (§6.1) Definition Applications Terminology Properties ADT Data structures.
Graphs1 ORD DFW SFO LAX Graphs2 Outline and Reading Graphs (§6.1) Definition Applications Terminology Properties ADT Data structures.
More Graph Algorithms Weiss ch Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.
ECE669 L10: Graph Applications March 2, 2004 ECE 669 Parallel Computer Architecture Lecture 10 Graph Applications.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
GRAPH Learning Outcomes Students should be able to:
Data Structures Using C++ 2E
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Discrete Maths 11. Graph Theory Objective
Graphs – ADTs and Implementations ORD DFW SFO LAX
Graphs Chapter 12.
Representing and Using Graphs
Foundations of Discrete Mathematics
CSE 326: Data Structures NP Completeness Ben Lerner Summer 2007.
Graphs CSE 2011 Winter June Graphs A graph is a pair (V, E), where  V is a set of nodes, called vertices  E is a collection of pairs.
Spring 2007Graphs1 ORD DFW SFO LAX
GRAPHS 1. Outline 2  Undirected Graphs and Directed Graphs  Depth-First Search  Breadth-First Search.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms.
ADA: 9. Graph Search1 Objective o describe and compare depth-first and breadth- first graph searching, and look at the creation of spanning trees.
CSE 373: Data Structures and Algorithms
Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Contest Algorithms January 2016 Introduce the main kinds of graphs, discuss two implementation approaches, and remind you about trees 6. Intro. to Graphs.
Contest Algorithms January 2016 Describe shortest path trees, SSSP, APSP, and three algorithms: Dijkstra, Bellman-Ford, Floyd-Warshall 9. Shortest Paths.
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Graphs + Shortest Paths David Kauchak cs302 Spring 2013.
I can describe the differences between Hamilton and Euler circuits and find efficient Hamilton circuits in graphs. Hamilton Circuits I can compare and.
CHAPTER 13 GRAPH ALGORITHMS ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
1 GRAPH Learning Outcomes Students should be able to: Explain basic terminology of a graph Identify Euler and Hamiltonian cycle Represent graphs using.
Programming Abstractions Cynthia Lee CS106B. Upcoming Topics Graphs! 1.Basics  What are they? How do we represent them? 2.Theorems  What are some things.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
1 Data Structures and Algorithms Graphs. 2 Graphs Basic Definitions Paths and Cycles Connectivity Other Properties Representation Examples of Graph Algorithms:
Graphs ORD SFO LAX DFW Graphs 1 Graphs Graphs
Graphs 10/24/2017 6:47 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Graphs.
Graphs Chapter 20.
Graphs 5/14/ :46 PM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Graphs 7/18/2018 7:39 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Graphs ORD SFO LAX DFW Graphs Graphs
CS120 Graphs.
CMSC 341 Lecture 21 Graphs (Introduction)
Discrete Maths 9. Graphs Objective
Graphs.
Graph.
Graphs CSE 2011 Winter November 2018.
Foundations of Discrete Mathematics
Chapter 11 Graphs.
Graphs 4/29/15 01:28:20 PM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Chapter 15 Graphs © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
CSE 373: Data Structures and Algorithms
Algorithms: Design and Analysis
Algorithms: Design and Analysis
Algorithms: Design and Analysis
Presentation transcript:

Discrete Maths 11. Graph Theory Objective 242-213, Semester 2, 2015-2016 11. Graph Theory Objective introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms (e.g. Dijkstra’s shortest path).

1. Graph Terms (Strongly) Connected Component Vertices/Nodes Sub Graph Complete Graph Directed Acyclic Graph (DAG) Euler/Hamiltonian Cycle Vertices/Nodes Edges Un/Weighted Un/Directed In/Out Degree Self-Loop/Multiple Edges Sparse/Dense Path, Cycle Isolated, Reachable Contest Algorithms: 6. Graph Intro

A graph has two parts (V, E), where: V are the nodes, called vertices E are the links between vertices, called edges 849 PVD 1843 ORD 142 SFO 802 LGA 1743 337 1387 HNL 2555 1099 LAX 1233 DFW 1120 MIA

Directed graph the edges are directed e.g., bus cost network Undirected graph the edges are undirected e.g., road network

A weighted graph adds edge numbers. 8 b a 6 2 6 4 c 3 d 5 9 12 4 e

End vertices (or endpoints) of an edge U and V are the endpoints Edges incident on a vertex a, d, and b are incident Adjacent vertices U and V are adjacent Degree of a vertex X has degree 5 Parallel edges h and i are parallel edges Self-loop j is a self-loop X U V W Z Y a c b e d f g h i j

Path Simple path Examples sequence of alternating vertices and edges begins with a vertex ends with a vertex each edge is preceded and followed by its endpoints Simple path path such that all its vertices and edges are distinct Examples P1=(V,b,X,h,Z) is a simple path P2=(U,c,W,e,X,g,Y,f,W,d,V) is a path that is not simple V a b P1 d U X Z P2 h c e W g f Y

Cycle Simple cycle Examples circular sequence of alternating vertices and edges each edge is preceded and followed by its endpoints Simple cycle cycle such that all its vertices and edges are distinct Examples C1=(V,b,X,g,Y,f,W,c,U,a) is a simple cycle C2=(U,c,W,e,X,g,Y,f,W,d,V,a,) is a cycle that is not simple V a b d U X Z C2 h e C1 c W g f Y Graphs

A graph is connected if there is a path between every pair of vertices Connected graph Non connected graph with two connected components

Strong Connectivity Each vertex can reach all other vertices a g c d e b e f g Graphs

Complete Graphs All pairs of vertices are connected by an edge. No. of edges |E| = |V| (|V-1|)/2 = O(|V|2)

Directed Acyclic Graph (DAG) A DAG has no cycles Some algorithms become simpler when used on DAGs instead of general graphs, based on the principle of topological ordering find shortest paths and longest paths by processing the vertices in a topological order Contest Algorithms

2. The Euler Cycle If a graph G has a simple cycle from vertex v to v, which uses every edge exactly once, only if there are an even number of edges connected to each vertex. The first graph theory result, proved by by Leonhard Euler in 1736 for the Konigsberg Bridge Problem.

Practical Uses of Euler Cycles In computer networks, edge traversal (i.e. moving between network nodes) is expensive. Euler’s definition is a very fast algorithm for checking whether a graph (network) can be traversed efficiently.

Example Can this network be traversed efficiently (e.g. by a Web search engine collecting information)? e.g. start at a, finish at a, travel each edge only once? a b c f e d g h i

3. Hamiltonian Cycle Sir William Rowan Hamilton’s puzzle (1850’s) it made him very rich Each corner is labelled with a city name. The shape is a dodecahedron. My version uses country names. continued

Problem: start at any city (letter), travel along the edges, visit each city exactly once, and return to the starting city. Note: not all edges need to be used

Graph of Hamilton’s Puzzle

Hamiltonian Cycle Formalised In a graph G, find a cycle that contains each vertex exactly once, except for the starting/ending vertex that appears twice.

A Solution not all edges used b a g f h p t i o q s r j c n e m k l d

Hamilton vs. Euler? A Euler cycle visits each edge once. A Hamiltonian cycle visits each vertex once. They sound similar, but mathematicians have much harder problems with Hamiltonian cycles e.g. it is easy to check for a Euler cycle, but there is no simple test for a Hamiltonian cycle

Algorithms for Finding Cycles There are algorithms for finding a Euler cycle in a graph which take O(a) time a is the number of edges in the graph Algorithms for finding a Hamiltonian cycle are O(ea) or O(a!) in the worst case much too slow for real graphs continued

For that reason, algorithms designed for real-world data only generate near-minimum length cycles they are less time consuming, but may not give the best answer

Some Properties of Hamiltonian Cycles 1) If a graph has N verticies, then the Hamiltonian cycle must use N edges. e.g. s a b t w v c d u 2) Every vertex in a Hamiltonian cycle has a degree of 2 (some edges may not be used).

Proving there is no H.C The graph has 5 verticies and 6 edges. v4 and v2 have degree 3. Must include the two edges connected to v1 and v3. This creates a loop, but without v5, so not a H.C. v1 v2 v5 v4 v3 continued

Proving there is no H.C b c a e d f i k g h j l m continued

Assume that the graph does have a H.C. Edges (a,b), (a,g), (b,c), (c,k) must be in the H.C. since verticies ‘a’, ‘c’ have degree 2. Therefore, edges (b,d), (b,f) must not be in the H.C. since ‘b’ is fully used already. continued

But there is now a cycle: Therefore, edges (g,d), (d,e), (e,f), (f,k) must be in the H,C,. since that is the only way to have ‘d’ and ‘f’ in the H.C. But there is now a cycle: {a, b, c, k, f, e, d, g, a} We cannot connect any more edges to g, e, k since their degree is 2 already so it is not possible to create a H.C.

4. The Travelling Salesman Problem This problem is related to the Hamiltonian cycle, but the graph is weighted see the sheet metal hole drilling example Given a weighted graph G, find a minimum length Hamiltonian cycle in G.

Example Answer: {a,b,c,d,a} with minimum length 11. Proof: try replacing any edge (e.g. (d,c) by either of the ‘long’ edges. b a 2 11 3 3 d 3 c 11

Why Travelling Salesman? Think of the verticies as cities, edge weights as distances. The problem becomes: find a shortest route in which a salesman (or woman) can visit each city once, starting and ending at the same city.

5. Depth First Search (DFS) Depth First Search (DFS) is a graph searching method, useful for directed graphs e.g. the Web DFS uses recursion to explore all the successors of a node, and backtracking to choice points. Problem: cycles Solution: DFS ‘marks’ nodes as it visits them, and never revisits marked nodes.

DFS is “depth first” because it always fully explores down a path away from a vertex v before it backtracks to looks at other paths (other choices) leaving v.

Directed Graph Example b d e f c Graph G

DFS Tree Since nodes are marked, the graph is searched as if it were a tree: a/1 b/2 d/4 c/3 e/5 f/6 c

Running Time The time taken to search from a node is proportional to the no. of successors of that node. Total search time for all nodes = O(n). Total search time for all successors = time to search all edges = O(a). Total running time is O(n + a) continued

If the graph is dense, a >> n, the O(n) term can be ignored in that case, the total running time = O(a)

Uses of DFS Finding cycles in a graph Reachability detection e.g. for finding recursive dependencies in a calling graph Reachability detection i.e. can a vertex v be reached from vertex u? useful for e-mail routing

6. Finding the Shortest Path A weighted graph has values (weights)assigned to its edges. The length of a path = the sum of the weights of the edges in the path w(i,j) = weight of edge (i,j) The shortest path between two verticies = the path having the minimum length.

Example Weighted Graph b 2 c 2 4 1 2 3 z d e a 4 3 7 1 6 f 5 g Problem: find the shortest path from vertex a to vertex z.

Dijkstra’s Shortest Path Algorithm Assign scores to verticies: S(v) = score of vertex v (some integer) there are temporary and permanent scores all verticies start with a temporary score of infinity (Inf) continued

The algorithm uses a set T, which contains all the nodes with temporary scores initially that is all n nodes When the score of a vertex v is made permanent, it is also the length of the shortest path from vertex a to vertex v this is what we want!

Algorithm (as C-like pseudocode) int dijkstra(vertex a, vertex z) // find the shortest path from a to z { // initialisation S(a) = 0; // 1 for (all verticies x != a) // 2 S(x) = Inf; // 3 T = all verticies; // 4 : continued

// find shortest path to z while (z in T) { // find shortest path to z while (z in T) { // 5 choose v from T with min S(v); // 6 T = T without v; // 7 for (each x in T adjacent to v) // 8 S(x) = smaller_of( S(x), S(v)+w(v,x) ); // 9 } // 10 return S(z); // return shortest path }

Processing the Example b 2 c 2 4 1 2 3 z d e a 4 3 7 1 6 f 5 g continued

Initialisation = temporary score Inf b 2 c Inf 2 4 1 2 3 Inf Inf z Inf Inf Inf z Inf d e a 4 3 7 1 6 f 5 g Inf Inf continued

First Iteration = permanent score = changed temporary 2 b 2 c Inf 2 4 1 2 3 Inf Inf d e Inf z a 4 3 7 1 6 f 5 g Inf 1 continued

Second Iteration 2 b 2 c Inf 2 4 1 2 3 4 Inf d e Inf z a 4 3 7 1 6 f 5 4 Inf d e Inf z a 4 3 7 1 6 f 5 g 6 1 continued

Third Iteration b 2 c 4 2 4 1 2 3 4 6 z Inf d e a 4 3 7 1 6 f 5 g 6 4 6 z Inf d e a 4 3 7 1 6 f 5 g 6 1 continued

Fourth Iteration b 2 c 2 4 1 2 3 4 z 5 d e 6 a 4 3 7 1 6 f 5 g 6 4 z 5 d e 6 a 4 3 7 1 6 f 5 g 6 1 Could have chosen ‘d’ instead. continued

Fifth Iteration b 2 c 2 4 1 2 3 z 5 d e 6 a 4 3 7 1 6 f 5 g 6 4 z 5 d e 6 a 4 3 7 1 6 f 5 g 6 1 Choosing ‘d’ is a waste of time. continued

Sixth (and final) Iteration 2 4 b 2 c 2 4 1 2 3 4 z 5 d e 6 a 4 3 7 1 6 Finished! Shortest path from ‘a’ to ‘z’ is length 5. f 5 g 6 1

Notes On each iteration: one score becomes permanent the vertex with the new permanent score changes its adjacent verticies’ temporary scores if they can be made smaller The algorithm eventually reaches every vertex.

Execution Time (worst case) The graph has n verticies. T is the set of verticies with temporary scores initially it contains all n verticies

Consider the Algorithm The loop on lines 2-3 is executed n-1 times it has O(n) execution time The loop on lines 5-10 is executed n times. The choose statement on line 6 will require a search through all of T in the worst case: so it has O(n) execution time continued

The loop on lines 8-9 looks through all of T: O(n) execution time Total execution time of loop on lines 5-10: O(n * (n+n)) = O(n2) Total execution time of function: O(n) + O(n2) = O(n2), for large n

7. Further Information Discrete Mathematics and its Applications Kenneth H. Rosen McGraw Hill, 2007, 7th edition chapter 10, sections 10.1, 10.2, 10.5, 10.6