© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.

Slides:



Advertisements
Similar presentations
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
Advertisements

22C:19 Discrete Math Graphs Fall 2014 Sukumar Ghosh.
Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
3.3 Spanning Trees Tucker, Applied Combinatorics, Section 3.3, by Patti Bodkin and Tamsen Hunter.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Chapter 8, Part I Graph Algorithms.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 27 Graph Applications.
Graph Search Methods Spring 2007 CSE, POSTECH. Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u. A search method.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
Data Structures Using C++
Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.
CS202 - Fundamental Structures of Computer Science II
CHAPTER 13 Graphs DATA ABSTRACTION AND PROBLEM SOLVING WITH C++ WALLS AND MIRRORS Third Edition Frank M. Carrano Janet J. Prichard Data Abstraction and.
ITEC200 – Week 12 Graphs. 2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study.
Graph Algorithms: Minimum Spanning Tree We are given a weighted, undirected graph G = (V, E), with weight function w:
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
Module #1 - Logic 1 Based on Rosen, Discrete Mathematics & Its Applications. Prepared by (c) , Michael P. Frank and Modified By Mingwu Chen Trees.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
22C:19 Discrete Math Graphs Spring 2014 Sukumar Ghosh.
Data Structures Using C++ 2E
MA/CSSE 473 Day 12 Insertion Sort quick review DFS, BFS Topological Sort.
Chapter 2 Graph Algorithms.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,
CS200 Algorithms and Data StructuresColorado State University Part 10. Graphs CS 200 Algorithms and Data Structures 1.
COSC 2007 Data Structures II Chapter 14 Graphs III.
Some material taken from website of Dale Winter.  Leonhard Euler – 1736  Find a starting point so that you can walk around the city, crossing each bridge.
1 Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting Yang,
Data Structures and Algorithms Ver. 1.0 Session 17 Objectives In this session, you will learn to: Implement a graph Apply graphs to solve programming problems.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
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.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Most of contents are provided by the website Graph Essentials TJTSD66: Advanced Topics in Social Media.
1 Subgraphs A subgraph S of a graph G is a graph such that The vertices of S are a subset of the vertices of G The edges of S are a subset of the edges.
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++
Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path.
1 Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting Yang,
COSC 2007 Data Structures II
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Graphs Upon completion you will be able to:
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Chapter 05 Introduction to Graph And Search Algorithms.
Graph Searching CSIT 402 Data Structures II. 2 Graph Searching Methodology Depth-First Search (DFS) Depth-First Search (DFS) ›Searches down one path as.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
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.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Graphs Spring 2016CS202 - Fundamental Structures of Computer Science II1 Initially prepared by Dr. Ilyas Cicekli; improved by various Bilkent CS202 instructors.
IOI/ACM ICPC Training 4 June 2005.
Graphs Chapter 20.
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Graphs.
CS202 - Fundamental Structures of Computer Science II
CS120 Graphs.
More Graph Algorithms.
Graphs Chapter 13.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
GRAPH – Definitions A graph G = (V, E) consists of
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs

© 2006 Pearson Addison-Wesley. All rights reserved14 B-2 Depth-First Search Depth-first search (DFS) traversal –Proceeds along a path from v as deeply into the graph as possible before backing up –Does not completely specify the order in which it should visit the vertices adjacent to v –A last visited, first explored strategy

© 2006 Pearson Addison-Wesley. All rights reserved14 B-3 Depth-First Search +dfs(in v:Vertex) //Traverses a graph beginning at vertex v by using a depth-first search: Recursive version Mark v as visited for(each unvisited vertex u adjacent to v) dfs(u)

© 2006 Pearson Addison-Wesley. All rights reserved5 B-4 Breadth-First Search Breadth-first search (BFS) traversal –Visits every vertex adjacent to a vertex v that it can before visiting any other vertex –A first visited, first explored strategy –An iterative form uses a queue –A recursive form is possible, but not simple

© 2006 Pearson Addison-Wesley. All rights reserved5 B-5 Implementing a BFS Iterator Class Using the JCF BFSIterator class uses the ListIterator class –As a queue to keep track of the order the vertices should be processed BFSIterator constructor –Initiates methods used to determine BFS order of vertices for the graph Graph is searched by processing vertices from each vertex’s adjacency list –In the order that they were pushed onto the queue

© 2006 Pearson Addison-Wesley. All rights reserved14 B-6 Applications of Graphs: Topological Sorting Topological order –A list of vertices in a directed graph without cycles such that vertex x precedes vertex y if there is a directed edge from x to y in the graph –There may be several topological orders in a given graph Topological sorting –Arranging the vertices into a topological order

© 2006 Pearson Addison-Wesley. All rights reserved14 B-7 Topological Sorting Figure The graph in Figure arranged according to the topological orders a) a, g, d, b, e, c, f and b) a, b, g, d, e, f, c Figure A directed graph without cycles

© 2006 Pearson Addison-Wesley. All rights reserved14 B-8 Topological Sorting Simple algorithms for finding a topological order –topSort1 Find a vertex that has no successor Remove from the graph that vertex and all edges that lead to it, and add the vertex to the beginning of a list of vertices Add each subsequent vertex that has no successor to the beginning of the list When the graph is empty, the list of vertices will be in topological order

© 2006 Pearson Addison-Wesley. All rights reserved14 B-9 Topological Sorting Simple algorithms for finding a topological order (Continued) –topSort2 A modification of the iterative DFS algorithm Strategy –Push all vertices that have no predecessor onto a stack –Each time you pop a vertex from the stack, add it to the beginning of a list of vertices –When the traversal ends, the list of vertices will be in topological order

© 2006 Pearson Addison-Wesley. All rights reserved14 B-10 Spanning Trees A tree –An undirected connected graph without cycles A spanning tree of a connected undirected graph G –A subgraph of G that contains all of G’s vertices and enough of its edges to form a tree To obtain a spanning tree from a connected undirected graph with cycles –Remove edges until there are no cycles

© 2006 Pearson Addison-Wesley. All rights reserved14 B-11 Spanning Trees You can determine whether a connected graph contains a cycle by counting its vertices and edges –A connected undirected graph that has n vertices must have at least n – 1 edges –A connected undirected graph that has n vertices and exactly n – 1 edges cannot contain a cycle –A connected undirected graph that has n vertices and more than n – 1 edges must contain at least one cycle

© 2006 Pearson Addison-Wesley. All rights reserved14 B-12 Spanning Trees Figure Connected graphs that each have four vertices and three edges

© 2006 Pearson Addison-Wesley. All rights reserved14 B-13 The DFS Spanning Tree To create a depth-first search (DFS) spanning tree –Traverse the graph using a depth-first search and mark the edges that you follow –After the traversal is complete, the graph’s vertices and marked edges form the spanning tree

© 2006 Pearson Addison-Wesley. All rights reserved14 B-14 The BFS Spanning Tree To create a breath-first search (BFS) spanning tree –Traverse the graph using a bread-first search and mark the edges that you follow –When the traversal is complete, the graph’s vertices and marked edges form the spanning tree

© 2006 Pearson Addison-Wesley. All rights reserved14 B-15 Minimum Spanning Trees Minimum spanning tree –A spanning tree for which the sum of its edge weights is minimal Prim’s algorithm –Finds a minimal spanning tree that begins at any vertex –Strategy Find the least-cost edge (v, u) from a visited vertex v to some unvisited vertex u Mark u as visited Add the vertex u and the edge (v, u) to the minimum spanning tree Repeat the above steps until there are no more unvisited vertices

© 2006 Pearson Addison-Wesley. All rights reserved14 B-16 Shortest Paths Shortest path between two vertices in a weighted graph –The path that has the smallest sum of its edge weights Dijkstra’s shortest-path algorithm –Determines the shortest paths between a given origin and all other vertices –Uses A set vertexSet of selected vertices An array weight, where weight[v] is the weight of the shortest (cheapest) path from vertex 0 to vertex v that passes through vertices in vertexSet

© 2006 Pearson Addison-Wesley. All rights reserved14 B-17 Circuits A circuit –A special cycle that passes through every vertex (or edge) in a graph exactly once Euler circuit –A circuit that begins at a vertex v, passes through every edge exactly once, and terminates at v –Exists if and only if each vertex touches an even number of edges Figure a) Euler’s bridge problem and b) its multigraph representation

© 2006 Pearson Addison-Wesley. All rights reserved14 B-18 Some Difficult Problems Three applications of graphs –The traveling salesperson problem –The three utilities problem –The four-color problem

© 2006 Pearson Addison-Wesley. All rights reserved14 B-19 The Four Colour Problem

© 2006 Pearson Addison-Wesley. All rights reserved14 B-20 The Four Colour Problem Given graph, a colouring is an assignment of colours to the vertices such that no adjacent vertices are the same colour Related: colouring a map so that adjacent countries are not the same colour Maps can be translated to planar graphs... graphs with no crossing edges

© 2006 Pearson Addison-Wesley. All rights reserved14 B-21 The Four Colour Problem From maps to graphs

© 2006 Pearson Addison-Wesley. All rights reserved14 B-22 The Four Colour Problem Colouring a geographic map

© 2006 Pearson Addison-Wesley. All rights reserved14 B-23 The Four Colour Problem How many colours are needed to colour a map? In 1852, Francis Guthrie observed that he only needed 4 colours to colour a map of the counties in England He conjectured that 4 is always enough

© 2006 Pearson Addison-Wesley. All rights reserved14 B-24 The Four Colour Problem After 124 years... a “proof” was produced (Appel and Haken, 1976) But the proof basically worked like this: –Find a whole bunch of important special cases that capture all important configurations [there were 1936 of these] –Prove that all of these cases can be solved

© 2006 Pearson Addison-Wesley. All rights reserved14 B-25 The Four Colour Problem The “proof” uses a computer to find the special cases... and it cannot be verified by hand The written part of the proof is 500 pages, and very tedious –Has never been completely verified

© 2006 Pearson Addison-Wesley. All rights reserved14 B-26 The Four Colour Problem Is this a proof? –If it can not be verified by hand, how do we know the computer found all the right cases? This sparked major foundational debate We know proof != truth... so even if the computer is right – do we have a proof? Debate has fizzled to some degree...

© 2006 Pearson Addison-Wesley. All rights reserved14 B-27 Summary The two most common implementations of a graph are the adjacency matrix and the adjacency list Graph searching –Depth-first search goes as deep into the graph as it can before backtracking –Bread-first search visits all possible adjacent vertices before traversing further into the graph Topological sorting produces a linear order of the vertices in a directed graph without cycles

© 2006 Pearson Addison-Wesley. All rights reserved14 B-28 Summary Trees are connected undirected graphs without cycles –A spanning tree of a connected undirected graph is a subgraph that contains all the graph’s vertices and enough of its edges to form a tree A minimum spanning tree for a weighted undirected graph is a spanning tree whose edge- weight sum is minimal The shortest path between two vertices in a weighted directed graph is the path that has the smallest sum of its edge weights

© 2006 Pearson Addison-Wesley. All rights reserved14 B-29 Summary An Euler circuit in an undirected graph is a cycle that begins at vertex v, passes through every edge in the graph exactly once, and terminates at v A Hamilton circuit in an undirected graph is a cycle that begins at vertex v, passes through every vertex in the graph exactly once, and terminates at v

© 2006 Pearson Addison-Wesley. All rights reserved14 B-30 Exercise Write pseudocode for an iterative algorithm that determines a DFS spanning tree for an undirected graph.

© 2006 Pearson Addison-Wesley. All rights reserved14 B-31 Solution dFSTree(v) // Forms a spanning tree for a connected, undirected graph // beginning at vertex v by using depth-first search: // Iterative version. s.createStack() // push v onto stack and mark it s.push(v) Mark v as visited //...cont’d

© 2006 Pearson Addison-Wesley. All rights reserved14 B-32 // loop invariant: there is a path from vertex v at the // bottom of the stack s to the vertex at the top of s while (!s.isEmpty()) { if (no unvisited vertices are adjacent to the vertex at the top of the stack) s.pop() // backtrack else { Select an unvisited vertex u adjacent to the vertex at the top of the stack Mark the edge from u to the vertex at the the top of the stack. s.push(u) Mark u as visited } // end if } // end while

© 2006 Pearson Addison-Wesley. All rights reserved14 B-33 Exercise Revise the topSort1 algorithm by removing predecessors instead of successors. Trace the new algorithm for this graph: a b c d e

© 2006 Pearson Addison-Wesley. All rights reserved14 B-34 Solution topSort1(g, l) // Arranges the vertices of g into a // topological order and places them in list l. n = number of vertices in g for(step = 1 through n) { select a vertex v that has no predecessors l.listInsert(step, v) Delete from g vertex v and its edges } // end for // end TopSort1

© 2006 Pearson Addison-Wesley. All rights reserved14 B-35 Solution a b c d e b c d e c d e c e c add a to list: a add b to list: a b add d to list: a b d add e to list: a b d e add c to list: a b d e c (done)

© 2006 Pearson Addison-Wesley. All rights reserved14 B-36 Exercise In the text it says that a recursive BFS algorithm is not straightforward. a)why is this true? b)Write the pseudocode for the recursive version.

© 2006 Pearson Addison-Wesley. All rights reserved14 B-37 Solution a) The recursive BFS algorithm is not simple because the underlying data structure is a queue. It must be a queue to ensure that vertices close to the current vertex are visited before those further away. DFS uses a stack and so can naturally be implemented recursively.

© 2006 Pearson Addison-Wesley. All rights reserved14 B-38 Solution (b) bFS(v) // Traverses a graph beginning at vertex v by using a // breadth-first search: Recursive version. q.createQueue() Mark v as visited for(each unvisited vertex u adjacent to v) q.enqueue(u) // loop invariant: there is a path from vertex v // to every vertex in the queue q while(!q.isEmpty()) { w=q.dequeue() bFS(w) } // end while