Chapter 14. Graphs Pearson Addison-Wesley. All rights reserved 14 A-1

Similar documents
The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value

CS302 - Data Structures using C++

Graphs. Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale Room - Faner 3131

UNIT 5 GRAPH. Application of Graph Structure in real world:- Graph Terminologies:

Outline. Graphs. Divide and Conquer.

Understand graph terminology Implement graphs using

Chapter 5. Decrease-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

UNIT Name the different ways of representing a graph? a.adjacencymatrix b. Adjacency list

LECTURE 17 GRAPH TRAVERSALS

Konigsberg Bridge Problem

Basic Graph Definitions

Homework Assignment #3 Graph

CSE 100 Minimum Spanning Trees Prim s and Kruskal

CSC Design and Analysis of Algorithms. Lecture 4 Brute Force, Exhaustive Search, Graph Traversal Algorithms. Brute-Force Approach

CS 206 Introduction to Computer Science II

CSS 343 Data Structures, Algorithms, and Discrete Math II. Graphs II. Yusuf Pisan

Module 11: Additional Topics Graph Theory and Applications

Graph. Vertex. edge. Directed Graph. Undirected Graph

Chapter 9. Greedy Technique. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

CS200: Graphs. Rosen Ch , 9.6, Walls and Mirrors Ch. 14

Graph Traversals. CS200 - Graphs 1

Outline. Introduction. Representations of Graphs Graph Traversals. Applications. Definitions and Basic Terminologies

Unweighted Graphs & Algorithms

Graphs & Digraphs Tuesday, November 06, 2007

Undirected Graphs. Hwansoo Han

Minimum Spanning Trees Ch 23 Traversing graphs

CS/COE 1501 cs.pitt.edu/~bill/1501/ Graphs

Graphs and Algorithms

MA/CSSE 473 Day 12. Questions? Insertion sort analysis Depth first Search Breadth first Search. (Introduce permutation and subset generation)

Module 2: NETWORKS AND DECISION MATHEMATICS


Chapter 9 Graph Algorithms

Introduction to Graphs. common/notes/ppt/

11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI. Department of Computer Science and Engineering CS6301 PROGRAMMING DATA STRUCTURES II

Graph: representation and traversal

Topics. Trees Vojislav Kecman. Which graphs are trees? Terminology. Terminology Trees as Models Some Tree Theorems Applications of Trees CMSC 302

Multidimensional Arrays & Graphs. CMSC 420: Lecture 3

Theory of Computing. Lecture 10 MAS 714 Hartmut Klauck

Further Mathematics 2016 Module 2: NETWORKS AND DECISION MATHEMATICS Chapter 9 Undirected Graphs and Networks

Trees. Arash Rafiey. 20 October, 2015

2. True or false: even though BFS and DFS have the same space complexity, they do not always have the same worst case asymptotic time complexity.

Week 9. CS 400 Programming III

Part VI Graph algorithms. Chapter 22 Elementary Graph Algorithms Chapter 23 Minimum Spanning Trees Chapter 24 Single-source Shortest Paths

SELF-BALANCING SEARCH TREES. Chapter 11

Graph Theory. Many problems are mapped to graphs. Problems. traffic VLSI circuits social network communication networks web pages relationship

ECE4050 Data Structures and Algorithms. Lecture 8: Graphs

Introduction to Computer Science and Programming for Astronomers

Graphs. The ultimate data structure. graphs 1

Lecture 9 Graph Traversal

Graph Algorithms. Chapter 22. CPTR 430 Algorithms Graph Algorithms 1

CS/COE

CSE 100: GRAPH ALGORITHMS

Advanced Data Structures and Algorithms

Graph definitions. There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs. An undirected graph

CS4800: Algorithms & Data Jonathan Ullman

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May

L10 Graphs. Alice E. Fischer. April Alice E. Fischer L10 Graphs... 1/37 April / 37

Graph Search Methods. Graph Search Methods

Graph Search Methods. Graph Search Methods

CS 310 Advanced Data Structures and Algorithms

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Graphs. Data Structures 1 Graphs

CS61BL. Lecture 5: Graphs Sorting

CSI 604 Elementary Graph Algorithms

W4231: Analysis of Algorithms

( ) ( ) C. " 1 n. ( ) $ f n. ( ) B. " log( n! ) ( ) and that you already know ( ) ( ) " % g( n) ( ) " #&

// Source code example for "A Practical Introduction to Data Structures and // Algorithm Analysis" by Clifford A. Shaffer, Prentice Hall, 1998.

Lecture 3: Graphs and flows

(Re)Introduction to Graphs and Some Algorithms

Worksheet for the Final Exam - Part I. Graphs

- Logic and Algorithms - Graph Algorithms

GRAPHICAL ALGORITHMS. UNIT _II Lecture-12 Slides No. 3-7 Lecture Slides No Lecture Slides No

R13. II B. Tech I Semester Supplementary Examinations, May/June DATA STRUCTURES (Com. to ECE, CSE, EIE, IT, ECC)

LECTURE 11 TREE TRAVERSALS

Algorithms and Data Structures (INF1) Lecture 15/15 Hua Lu

GRAPHS Lecture 19 CS2110 Spring 2013

CSC Intro to Intelligent Robotics, Spring Graphs

GRAPHS Lecture 17 CS2110 Spring 2014

10/31/18. About A6, Prelim 2. Spanning Trees, greedy algorithms. Facts about trees. Undirected trees

Spanning Trees, greedy algorithms. Lecture 20 CS2110 Fall 2018

Index. stack-based, 400 A* algorithm, 325

DS UNIT 4. Matoshri College of Engineering and Research Center Nasik Department of Computer Engineering Discrete Structutre UNIT - IV

CSC 172 Data Structures and Algorithms. Lecture 24 Fall 2017

CSE 373 NOVEMBER 20 TH TOPOLOGICAL SORT

Elementary Graph Algorithms. Ref: Chapter 22 of the text by Cormen et al. Representing a graph:

Computer Science and Software Engineering University of Wisconsin - Platteville. 3. Search (Part 1) CS 3030 Lecture Notes Yan Shi UW-Platteville

CS24 Week 8 Lecture 1

CSCE 350: Chin-Tser Huang. University of South Carolina

Representations of Graphs

Graphs II: Trailblazing

The Konigsberg Bridge Problem

Spanning Trees, greedy algorithms. Lecture 22 CS2110 Fall 2017

Today s Outline. CSE 326: Data Structures. Topic #15: Cool Graphs n Pretty Pictures. A Great Mathematician. The Bridges of Königsberg

Trees Algorhyme by Radia Perlman

1. Graph and Representation

Breadth First Search. Graph Traversal. CSE 2011 Winter Application examples. Two common graph traversal algorithms

Chapter 14 Section 3 - Slide 1

Graphs. Pseudograph: multiple edges and loops allowed

L22-23: Graph Algorithms

Transcription:

Chapter 14 Graphs 2011 Pearson Addison-Wesley. All rights reserved 14 A-1

Terminology G = {V, E} A graph G consists of two sets A set V of vertices, or nodes A set E of edges A subgraph Consists of a subset of a graph s vertices and a subset of its edges Adjacent vertices Two vertices that are joined by an edge 2011 Pearson Addison-Wesley. All rights reserved 14 A-2

Terminology Figure 14-2 a) A campus map as a graph; b) a subgraph 2011 Pearson Addison-Wesley. All rights reserved 14 A-3

Terminology A path between two vertices A sequence of edges that begins at one vertex and ends at another vertex May pass through the same vertex more than once A simple path A path that passes through a vertex only once A cycle A path that begins and ends at the same vertex A simple cycle A cycle that does not pass through a vertex more than once 2011 Pearson Addison-Wesley. All rights reserved 14 A-4

Terminology A connected graph A graph that has a path between each pair of distinct vertices A disconnected graph A graph that has at least one pair of vertices without a path between them A complete graph A graph that has an edge between each pair of distinct vertices 2011 Pearson Addison-Wesley. All rights reserved 14 A-5

Terminology Figure 14-3 Graphs that are a) connected; b) disconnected; and c) complete 2011 Pearson Addison-Wesley. All rights reserved 14 A-6

Terminology Multigraph Not a graph Allows multiple edges between vertices Figure 14-4 a) A multigraph is not a graph; b) a self edge is not allowed in a graph 2011 Pearson Addison-Wesley. All rights reserved 14 A-7

Terminology Weighted graph A graph whose edges have numeric labels Figure 14-5a a) A weighted graph 2011 Pearson Addison-Wesley. All rights reserved 14 A-8

Terminology Undirected graph Edges do not indicate a direction Directed graph, or diagraph Each edge is a directed edge Figure 14-5b b) A directed graph 2011 Pearson Addison-Wesley. All rights reserved 14 A-9

Terminology Directed graph Can have two edges between a pair of vertices, one in each direction Directed path A sequence of directed edges between two vertices Vertex y is adjacent to vertex x if There is a directed edge from x to y 2011 Pearson Addison-Wesley. All rights reserved 14 A-10

Graphs As ADTs Graphs can be used as abstract data types Two options for defining graphs Vertices contain values Vertices do not contain values Operations of the ADT graph Create an empty graph Determine whether a graph is empty Determine the number of vertices in a graph Determine the number of edges in a graph 2011 Pearson Addison-Wesley. All rights reserved 14 A-11

Graphs As ADTs Operations of the ADT graph (Continued) Determine whether an edge exists between two given vertices; for weighted graphs, return weight value Insert a vertex in a graph whose vertices have distinct search keys that differ from the new vertex s search key Insert an edge between two given vertices in a graph Delete a particular vertex from a graph and any edges between the vertex and other vertices Delete the edge between two given vertices in a graph Retrieve from a graph the vertex that contains a given search key 2011 Pearson Addison-Wesley. All rights reserved 14 A-12

Implementing Graphs Most common implementations of a graph Adjacency matrix Adjacency list Adjacency matrix Adjacency matrix for a graph with n vertices numbered 0, 1,, n 1 An n by n array matrix such that matrix[i][j] is 1 (or true) if there is an edge from vertex i to vertex j 0 (or false) if there is no edge from vertex i to vertex j 2011 Pearson Addison-Wesley. All rights reserved 14 A-13

Implementing Graphs Figure 14-6 a) A directed graph and b) its adjacency matrix 2011 Pearson Addison-Wesley. All rights reserved 14 A-14

Implementing Graphs Adjacency matrix for a weighted graph with n vertices numbered 0, 1,, n 1 An n by n array matrix such that matrix[i][j] is The weight that labels the edge from vertex i to vertex j if there is an edge from i to j if there is no edge from vertex i to vertex j Figure 14-7 a) A weighted undirected graph and b) its adjacency matrix 2011 Pearson Addison-Wesley. All rights reserved 14 A-15

Implementing Graphs Adjacency list An adjacency list for a graph with n vertices numbered 0, 1,, n 1 Consists of n linked lists The i th linked list has a node for vertex j if and only if the graph contains an edge from vertex i to vertex j This node can contain either» Vertex j s value, if any» An indication of vertex j s identity 2011 Pearson Addison-Wesley. All rights reserved 14 A-16

Implementing Graphs Figure 14-8 a) A directed graph and b) its adjacency list 2011 Pearson Addison-Wesley. All rights reserved 14 A-17

Implementing Graphs Adjacency list for an undirected graph Treats each edge as if it were two directed edges in opposite directions Figure 14-9 a) A weighted undirected graph and b) its adjacency list 2011 Pearson Addison-Wesley. All rights reserved 14 A-18

Implementing Graphs Adjacency matrix compared with adjacency list Two common operations on graphs Determine whether there is an edge from vertex i to vertex j Find all vertices adjacent to a given vertex i Adjacency matrix Supports operation 1 more efficiently Adjacency list Supports operation 2 more efficiently Often requires less space than an adjacency matrix 2011 Pearson Addison-Wesley. All rights reserved 14 A-19

Implementing a Graph Class Using the JCF ADT graph not part of JCF Can implement a graph using an adjacency list consisting of a vector of maps Implementation presented uses TreeSet class 2011 Pearson Addison-Wesley. All rights reserved 5 B-20

Graph Traversals A graph-traversal algorithm Visits all the vertices that it can reach Visits all vertices of the graph if and only if the graph is connected A connected component The subset of vertices visited during a traversal that begins at a given vertex Can loop indefinitely if a graph contains a loop To prevent this, the algorithm must Mark each vertex during a visit, and Never visit a vertex more than once 2011 Pearson Addison-Wesley. All rights reserved 14 A-21

Graph Traversals Figure 14-10 Visitation order for a) a depth-first search; b) a breadth-first search 2011 Pearson Addison-Wesley. All rights reserved 14 A-22

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 2011 Pearson Addison-Wesley. All rights reserved 14 B-23

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 2011 Pearson Addison-Wesley. All rights reserved 14 B-24

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 2011 Pearson Addison-Wesley. All rights reserved 14 B-25

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 2011 Pearson Addison-Wesley. All rights reserved 14 B-26

Topological Sorting Figure 14-14 A directed graph without cycles Figure 14-15 The graph in Figure 14-14 arranged according to the topological orders a) a, g, d, b, e, c, f and b) a, b, g, d, e, f, c 2011 Pearson Addison-Wesley. All rights reserved 14 B-27

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 2011 Pearson Addison-Wesley. All rights reserved 14 B-28

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 2011 Pearson Addison-Wesley. All rights reserved 14 B-29

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 2011 Pearson Addison-Wesley. All rights reserved 14 B-30

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 2011 Pearson Addison-Wesley. All rights reserved 14 B-31

Spanning Trees Figure 14-19 Connected graphs that each have four vertices and three edges 2011 Pearson Addison-Wesley. All rights reserved 14 B-32

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 2011 Pearson Addison-Wesley. All rights reserved 14 B-33

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 2011 Pearson Addison-Wesley. All rights reserved 14 B-34

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 2011 Pearson Addison-Wesley. All rights reserved 14 B-35

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 2011 Pearson Addison-Wesley. All rights reserved 14 B-36

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 14-27 a) Euler s bridge problem and b) its multigraph representation 2011 Pearson Addison-Wesley. All rights reserved 14 B-37

Some Difficult Problems Three applications of graphs The traveling salesperson problem The three utilities problem The four-color problem A Hamilton circuit Begins at a vertex v, passes through every vertex exactly once, and terminates at v 2011 Pearson Addison-Wesley. All rights reserved 14 B-38

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 2011 Pearson Addison-Wesley. All rights reserved 14 B-39

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 edgeweight 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 2011 Pearson Addison-Wesley. All rights reserved 14 B-40

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 2011 Pearson Addison-Wesley. All rights reserved 14 B-41