Graphs Chapter 20.

Slides:



Advertisements
Similar presentations
Graphs Chapter Chapter Contents Some Examples and Terminology Road Maps Airline Routes Mazes Course Prerequisites Trees Traversals Breadth-First.
Advertisements

Graphs. 2 Some Examples and Terminology A graph is a set of vertices (nodes) and a set of edges (arcs) such that each edge is associated with exactly.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Chapter 20: Graphs CS Data Structures Mehmet H Gunes Modified from authors’ slides.
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.
Graphs Chapter 30 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
Data Structures Using C++
CHAPTER 13 Graphs DATA ABSTRACTION AND PROBLEM SOLVING WITH C++ WALLS AND MIRRORS Third Edition Frank M. Carrano Janet J. Prichard Data Abstraction and.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
ITEC200 – Week 12 Graphs. 2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study.
Data Structures & Algorithms Graph Search Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
Graphs Chapter 30 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 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.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
CISC220 Fall 2009 James Atlas Nov 13: Graphs, Line Intersections.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Graphs Chapter 28 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Data Structures Using C++ 2E
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
© 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,
CS 200 Algorithms and Data Structures
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.
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.
Graphs Chapter Chapter Contents Some Examples and Terminology Road Maps Airline Routes Mazes Course Prerequisites Trees Traversals Breadth-First.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
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. Contents Terminology Graphs as ADTs Applications of Graphs.
Graphs Chapter 28 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
A vertex u is reachable from vertex v iff there is a path from v to u.
Graphs.
Graphs Representation, BFS, DFS
CSE 373 Topological Sort Graph Traversals
Graphs CS Data Structures Mehmet H Gunes
Csc 2720 Instructor: Zhuojun Duan
Introduction to Graphs
CS202 - Fundamental Structures of Computer Science II
CS120 Graphs.
Comp 245 Data Structures Graphs.
Graph Operations And Representation
Graph.
Refresh and Get Ready for More
Graph Algorithm.
Graph Theory.
Graphs Chapter 13.
Graphs Chapter 15 explain graph-based algorithms Graph definitions
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Graph Representation (23.1/22.1)
Graphs.
Chapter 11 Graphs.
Yan Shi CS/SE 2630 Lecture Notes
A path that uses every vertex of the graph exactly once.
Spanning Trees Longin Jan Latecki Temple University based on slides by
Chapter 14 Graphs © 2006 Pearson Addison-Wesley. All rights reserved.
Ch. 14 Graphs Graph traversals Applications of graphs
GRAPHS G=<V,E> Adjacent vertices Undirected graph
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
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:

Graphs Chapter 20

FIGURE 20-1 An ordinary line graph Terminology Graphs represent relations among data items G = { V, E} A graph is a set of vertices (nodes) and A set of edges that connect the vertices FIGURE 20-1 An ordinary line graph

Terminology FIGURE 20-2 A graph and one of its subgraphs

Terminology FIGURE 20-3 Examples of undirected graphs that are either connected, disconnected, or complete

Terminology FIGURE 20-4 Graph-like structures that are not graphs

Terminology FIGURE 20-5 Examples of two kinds of graphs – an undirected weighted graph

Terminology FIGURE 20-5 Examples of two kinds of graphs –a directed weighted graph

Graphs as ADTs What data structure would you use? What operations should be supplied?

Graphs as ADTs ADT graph operations Test if empty Get number of vertices, edges in a graph See if edge exists between two given vertices Add vertex to graph whose vertices have distinct, different values from new vertex Add/remove edge between two given vertices Remove vertex, edges to other vertices Retrieve vertex that contains given value

Graphs as ADTs- Abstract Interface LISTING 20-1 A C++ interface for undirected, connected graphs

Graphs as ADTs LISTING 20-1 A C++ interface for undirected, connected graphs

Graphs as ADTs LISTING 20-1 A C++ interface for undirected, connected graphs

Implementing Graphs FIGURE 20-7 (a) A directed unweighted graph and (b) its adjacency matrix

Implementing Graphs FIGURE 20-8 (a) A weighted undirected graph and (b) its adjacency matrix

Implementing Graphs FIGURE 20-9 (a) A directed graph and (b) its adjacency list

Implementing Graphs FIGURE 20-10 (a) A weighted undirected graph and (b) its adjacency list

Implementing Graphs Adjacency list Adjacency matrix Often requires less space than adjacency matrix Supports finding vertices adjacent to given vertex Adjacency matrix Most easily supports process of seeing if there exists an edge from vertex i to vertex j

Graph Traversals Visits all vertices it can reach Visits all vertices if and only if the graph is connected Connected component Subset of vertices visited during a traversal that begins at a given vertex

Depth-First Search DFS traversal Goes as far as possible from a vertex before backing up Recursive traversal algorithm or stack based algorithm

Breadth-First Search BFS traversal Visits all vertices adjacent to a vertex before going forward BFS is a first visited, first explored strategy uses a queue based strategy Contrast DFS as last visited, first explored

Depth-First Search FIGURE 20-13 The results of a depth-first traversal, beginning at vertex a, of the graph in Figure 20-12

Breadth-First Search FIGURE 20-14 The results of a breadth-first traversal, beginning at vertex a, of the graph in Figure 20-12

Applications of Graphs Topological Sorting – finding an order of precedence relationships Spanning Tree – find a connected components Minimum Spanning Trees – find a minimum weight connected component Shortest Paths – find the shortest path from source to destination, find all pairs shortest paths Circuits – does a graph have a cycle Some Difficult Problems – Traveling Salesman Problem, Hamiltonian Circuit Problem, Euler Circuit Problem, Finding a k-Clique in a graph, etc…..

Topological Sorting FIGURE 20-15 A directed graph without cycles

Topological Sorting FIGURE 20-16 The graph in Figure 20-15 arranged according to two topological orders

Topological Sorting

Topological Sorting This is an incorrect DFS! Why? FIGURE 20-18 A trace of topSort2 for the graph in Figure 20-15 using depth-first search

Spanning Trees A tree is an undirected connected graph without cycles Detecting a cycle in an undirected graph Connected undirected graph with n vertices must have at least n – 1 edges If it has exactly n – 1 edges, it cannot contain a cycle With more than n – 1 edges, must contain at least one cycle

Spanning Trees FIGURE 20-19 A spanning tree for the graph in Figure 20-12. Dotted edges not in spanning tree.

Spanning Trees FIGURE 20-20 Connected graphs that each have four vertices and three edges

Spanning Trees Another possible spanning tree for the graph in Figure 20-12.

Spanning Trees FIGURE 20-23 The BFS spanning tree rooted at vertex a for the graph in Figure 20-12

Minimum Spanning Trees FIGURE 20-24 A weighted, connected, undirected graph

Minimum Spanning Trees FIGURE 20-25 A trace of primsAlgorithm for the graph in Figure 20-23, beginning at vertex a

Minimum Spanning Trees FIGURE 20-25 A trace of primsAlgorithm for the graph in Figure 20-23, beginning at vertex a

Minimum Spanning Trees FIGURE 20-25 A trace of primsAlgorithm for the graph in Figure 20-23, beginning at vertex a

Shortest Paths The shortest path between two vertices in a weighted graph Has the smallest edge-weight sum FIGURE 20-26 (a) A weighted directed graph and (b) its adjacency matrix

Shortest Paths FIGURE 20-27 A trace of the shortest-path algorithm applied to the graph in Figure 20-25a

Shortest Paths FIGURE 20-28 Checking weight[u] by examining the graph: (a) weight[2] in step 2; (b) weight[1] in step 3;

Shortest Paths FIGURE 20-28 Checking weight[u] by examining the graph: (c) weight[3] in step 3; (d) weight[3] in step 4

Circuits Circuit Another name for a type of cycle common in statement of certain types of problems Typical circuits either visit every vertex once or every edge once Euler Circuit – in P Begins at vertex v Passes through every edge exactly once Terminates at v Hamiltonian Circuit - in NP Complete Passes through every vertex exactly once

Circuits FIGURE 20-28 (a) Euler’s bridge problem and (b) its multigraph representation

Circuits FIGURE 20-29 Pencil and paper drawings

Circuits FIGURE 20-30 Connected undirected graphs based on the drawings in Figure 20-29

Circuits FIGURE 20-31 The steps to find an Euler circuit for the graph in Figure 20-30b

Circuits FIGURE 20-31 The steps to find an Euler circuit for the graph in Figure 20-30b

Circuits FIGURE 20-31 The steps to find an Euler circuit for the graph in Figure 20-30b

Some Difficult Problems Hamilton circuit Begins at vertex v Passes through every vertex exactly once Terminates at v Variation is “traveling salesperson problem” Visit every city on his route exactly once Edge (road) has associated cost (mileage) Goal is determine least expensive circuit

End Chapter 20