© 2010 Goodrich, Tamassia Graphs1 Graph Data Structures ORD DFW SFO LAX 802 1743 1843 1233 337.

Slides:



Advertisements
Similar presentations
Mathematical Preliminaries
Advertisements

Graph Algorithms Algorithm Design and Analysis Victor AdamchikCS Spring 2014 Lecture 11Feb 07, 2014Carnegie Mellon University.
© 2004 Goodrich, Tamassia Depth-First Search1 DB A C E.
© 2010 Goodrich, Tamassia Graphs1 Graph Data Structures ORD DFW SFO LAX
© 2004 Goodrich, Tamassia Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO.
CS203 Lecture 15.
© 2004 Goodrich, Tamassia Campus Tour1. © 2004 Goodrich, Tamassia Campus Tour2 Graph Assignment Goals Learn and implement the adjacency matrix structure.
Directed Graphs Directed Graphs Shortest Path 4/10/ :45 AM BOS
Bioinformatics Programming 1 EE, NCKU Tien-Hao Chang (Darby Chang)
CS16: Introduction to Data Structures & Algorithms
Depth-First Search1 Part-H2 Depth-First Search DB A C E.
CSC401 – Analysis of Algorithms Lecture Notes 14 Graph Biconnectivity
© 2004 Goodrich, Tamassia Breadth-First Search1 CB A E D L0L0 L1L1 F L2L2.
CSC 213 ORD DFW SFO LAX Lecture 20: Graphs.
© 2004 Goodrich, Tamassia Graphs1 ORD DFW SFO LAX
Breadth-First Search1 Part-H3 Breadth-First Search CB A E D L0L0 L1L1 F L2L2.
© 2004 Goodrich, Tamassia Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO.
© 2004 Goodrich, Tamassia Depth-First Search1 DB A C E.
TTIT33 Alorithms and Optimization – DALG Lecture 4 Graphs HT TTIT33 Algorithms and optimization Algorithms Lecture 4 Graphs.
1 Graphs: Concepts, Representation, and Traversal CSC401 – Analysis of Algorithms Lecture Notes 13 Graphs: Concepts, Representation, and Traversal Objectives:
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.
CSC311: Data Structures 1 Chapter 13: Graphs I Objectives: Graph ADT: Operations Graph Implementation: Data structures Graph Traversals: DFS and BFS Directed.
TDDB56 DALGOPT-D TDDB57 DALG-C – Lecture 11 – Graphs Graphs HT TDDB56 – DALGOPT-D Algorithms and optimization Lecture 11 Graphs.
Graphs – ADTs and Implementations ORD DFW SFO LAX
Graphs Part 1. Outline and Reading Graphs (§13.1) – Definition – Applications – Terminology – Properties – ADT Data structures for graphs (§13.2) – Edge.
Graphs. Data Structure for Graphs. Graph Traversals. Directed Graphs. Shortest Paths. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013.
1 Applications of BFS and DFS CSE 2011 Winter May 2016.
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.
GRAPHS 1. Outline 2  Undirected Graphs and Directed Graphs  Depth-First Search  Breadth-First Search.
1 prepared from lecture material © 2004 Goodrich & Tamassia COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material.
CISC 235: Topic 9 Introduction to Graphs. CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals.
Depth-First Search1 DB A C E. 2 Depth-first search (DFS) is a general technique for traversing a graph A DFS traversal of a graph G – Visits all the vertices.
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.
Depth-First Search Lecture 21: Graph Traversals
Graphs and Paths : Chapter 15 Saurav Karmakar
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
CHAPTER 13 GRAPH ALGORITHMS ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
CSC 213 – Large Scale Programming Lecture 31: Graph Traversals.
1 COMP9024: Data Structures and Algorithms Week Eleven: Graphs (I) Hui Wu Session 1, 2016
© 2010 Goodrich, Tamassia Breadth-First Search1 CB A E D L0L0 L1L1 F L2L2.
Graphs ORD SFO LAX DFW Graphs 1 Graphs Graphs
Breadth-First Search L0 L1 L2
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 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.
Searching Graphs ORD SFO LAX DFW Spring 2007
Breadth-First Search L0 L1 L2 C B A E D F Breadth-First Search
Breadth-First Search L0 L1 L2
Chapter 14 Graph Algorithms
Graphs Part 1.
Graphs.
Graphs.
Breadth-First Search L0 L1 L2 C B A E D F Breadth-First Search
Breadth-First Search L0 L1 L2 C B A E D F Breadth-First Search
Breadth-First Search (BFS)
Graphs CSE 2011 Winter November 2018.
Graphs ORD SFO LAX DFW Graphs Graphs
Subgraphs, Connected Components, Spanning Trees
Graphs ORD SFO LAX DFW Graphs 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,
Searching Graphs ORD SFO LAX DFW Spring 2007
Graphs Part 1 ORD SFO LAX DFW
Breadth-First Search L0 L1 L2 C B A E D F 4/25/2019 3:12 AM
Depth-First Search CSE 2011 Winter April 2019.
Applications of BFS CSE 2011 Winter /17/2019 7:03 AM.
Breadth-First Search L0 L1 L2 C B A E D F 5/14/ :22 AM
Breadth-First Search L0 L1 L2 C B A E D F 7/28/2019 1:03 PM
Presentation transcript:

© 2010 Goodrich, Tamassia Graphs1 Graph Data Structures ORD DFW SFO LAX

© 2010 Goodrich, Tamassia Graphs2 Main Methods of the Graph ADT  Vertices and edges are positions store elements  Accessor methods endVertices(e): an array of the two endvertices of e opposite(v, e): the vertex opposite of v on e areAdjacent(v, w): true iff v and w are adjacent replace(v, x): replace element at vertex v with x replace(e, x): replace element at edge e with x  Update methods insertVertex(o): insert a vertex storing element o insertEdge(v, w, o): insert an edge (v,w) storing element o removeVertex(v): remove vertex v (and its incident edges) removeEdge(e): remove edge e  Iterable collection methods incidentEdges(v): edges incident to v vertices(): all vertices in the graph edges(): all edges in the graph

© 2010 Goodrich, Tamassia Graphs3 Adjacency Matrix Structure  Edge list structure  Augmented vertex objects Integer key (index) associated with vertex  2D-array adjacency array Reference to edge object for adjacent vertices Null for non nonadjacent vertices  The “old fashioned” version just has 0 for no edge and 1 for edge u v w ab  1  2  a uv w 01 2 b

© 2010 Goodrich, Tamassia Graphs4 Adjacency List Structure  Incidence sequence for each vertex sequence of references to edge objects of incident edges  Augmented edge objects references to associated positions in incidence sequences of end vertices u v w ab a uv w b

© 2010 Goodrich, Tamassia Graphs5 Performance  n vertices, m edges  no parallel edges  no self-loops Edge List Adjacency List Adjacency Matrix Space n  m n2n2 incidentEdges( v ) mdeg(v)n areAdjacent ( v, w ) mmin(deg(v), deg(w))1 insertVertex( o ) 11n2n2 insertEdge( v, w, o ) 111 removeVertex( v ) mdeg(v)n2n2 removeEdge( e ) 111

6 Applications of BFS and DFS CSE 2011 Winter August 2014

7 Some Applications of BFS and DFS BFS  To find the shortest path from a vertex s to a vertex v in an unweighted graph  To find the length of such a path  To find out if a graph contains cycles  To construct a BSF tree/forest from a graph DFS  To find a path from a vertex s to a vertex v.  To find the length of such a path.  To construct a DSF tree/forest from a graph.

8 Testing for Cycles

9 Method isCyclic(v) returns true if a directed graph (with only one component) contains a cycle, and returns false otherwise. else return true; return false;

10 Finding Cycles To output the cycle just detected, use info in prev[ ]. NOTE: The code above applies only to directed graphs. Homework: Explain why that code does not work for undirected graphs.

Finding Cycles in Undirected Graphs To detect/find cycles in an undirected graph, we need to classify the edges into 3 categories during program execution:  unvisited edge: never visited.  discovery edge: visited for the very first time.  cross edge: edge that forms a cycle. Code fragment 13.10, p When the BFS algorithm terminates, the discovery edges form a spanning tree. If there exists a cross edge, the undirected graph contains a cycle. 11

BFS Algorithm (in textbook) Breadth-First Search 12 The algorithm uses a mechanism for setting and getting “labels” of vertices and edges Algorithm BFS(G, s) L 0  new empty sequence L 0.insertLast(s) setLabel(s, VISITED ) i  0 while  L i.isEmpty() L i  1  new empty sequence for all v  L i.elements() for all e  G.incidentEdges(v) if getLabel(e)  UNEXPLORED w  opposite(v,e) if getLabel(w)  UNEXPLORED setLabel(e, DISCOVERY ) setLabel(w, VISITED ) L i  1.insertLast(w) else setLabel(e, CROSS ) i  i  1 Algorithm BFS(G) Input graph G Output labeling of the edges and partition of the vertices of G for all u  G.vertices() setLabel(u, UNEXPLORED ) for all e  G.edges() setLabel(e, UNEXPLORED ) for all v  G.vertices() if getLabel(v)  UNEXPLORED BFS(G, v)

Example Breadth-First Search 13 C B A E D discovery edge cross edge A visited vertex A unexplored vertex unexplored edge L0L0 L1L1 F CB A E D L0L0 L1L1 F CB A E D L0L0 L1L1 F

Example (2) Breadth-First Search 14 CB A E D L0L0 L1L1 F CB A E D L0L0 L1L1 F L2L2 CB A E D L0L0 L1L1 F L2L2 CB A E D L0L0 L1L1 F L2L2

Example (3) Breadth-First Search 15 CB A E D L0L0 L1L1 F L2L2 CB A E D L0L0 L1L1 F L2L2 CB A E D L0L0 L1L1 F L2L2

16 Computing Spanning Trees

17 Trees Tree: a connected graph without cycles. Given a connected graph, remove the cycles  a tree. The paths found by BFS(s) form a rooted tree (called a spanning tree), with the starting vertex as the root of the tree. BFS tree for vertex s = 2 What would a level-order traversal of the tree tell you?

18 Computing a BFS Tree Use BFS on a vertex BFS( v ) with array prev[ ] The paths from source s to the other vertices form a tree

19 Computing Spanning Forests

20 Computing a BFS Forest A forest is a set of trees. A connected graph gives a tree (which is itself a forest). A connected component also gives us a tree. A graph with k components gives a forest of k trees.

21 Example D E A C F B G K H L N M O R Q P s A graph with 3 components

22 Example of a Forest D E A C F B G K H L N M O R Q P s A forest with 3 trees We removed the cycles from the previous graph.

23 Computing a BFS Forest Use BFS method on a graph BFSearch( G ), which calls BFS( v ) Use BFS( v ) with array prev[ ].  The paths originating from v form a tree. BFSearch( G ) examines all the components to compute all the trees in the forest.

24 Applications of DFS Is there a path from source s to a vertex v? Is an undirected graph connected? Is a directed graph strongly connected? To output the contents (e.g., the vertices) of a graph To find the connected components of a graph To find out if a graph contains cycles and report cycles. To construct a DSF tree/forest from a graph

Finding Cycles Using DFS Similar to using BFS. For undirected graphs, classify the edges into 3 categories during program execution: unvisited edge, discovery edge, and back (cross) edge.  Code Fragment 13.1, p  If there exists a back edge, the undirected graph contains a cycle. 25

© 2010 Goodrich, Tamassia Depth-First Search26 DFS Algorithm  The algorithm uses a mechanism for setting and getting “labels” of vertices and edges Algorithm DFS(G, v) Input graph G and a start vertex v of G Output labeling of the edges of G in the connected component of v as discovery edges and back edges setLabel(v, VISITED) for all e  G.incidentEdges(v) if getLabel(e)  UNEXPLORED w  opposite(v,e) if getLabel(w)  UNEXPLORED setLabel(e, DISCOVERY) DFS(G, w) else setLabel(e, BACK) Algorithm DFS(G) Input graph G Output labeling of the edges of G as discovery edges and back edges for all u  G.vertices() setLabel(u, UNEXPLORED) for all e  G.edges() setLabel(e, UNEXPLORED) for all v  G.vertices() if getLabel(v)  UNEXPLORED DFS(G, v)

© 2010 Goodrich, Tamassia Depth-First Search27 Example DB A C ED B A C ED B A C E discovery edge back edge A visited vertex A unexplored vertex unexplored edge

© 2010 Goodrich, Tamassia Depth-First Search28 Example (cont.) DB A C E DB A C E DB A C E D B A C E

29 DFS Tree Resulting DFS-tree. Notice it is much “deeper” than the BFS tree. Captures the structure of the recursive calls: - when we visit a neighbor w of v, we add w as child of v - whenever DFS returns from a vertex v, we climb up in the tree from v to its parent

30 Applications – DFS vs. BFS What can BFS do and DFS can’t?  Finding shortest paths (in unweighted graphs) What can DFS do and BFS can’t?  Finding out if a connected undirected graph is biconnected A connected undirected graph is biconnected if there are no vertices whose removal disconnects the rest of the graph. Application in computer networks: ensuring that a network is still connected when a router/link fails.

A graph that is not biconnected 31

32 DFS vs. BFS CB A E D L0L0 L1L1 F L2L2 CB A E D F DFSBFS ApplicationsDFSBFS Spanning forest, connected components, paths, cycles  Shortest paths  Biconnected components 

33 Final Exam  Sunday, April 17, 10:00-13:00 Materials:  All lectures notes and corresponding sections in the textbook from the beginning to today’s lecture  Homework questions  Assignments 1 to 5