46
Spring 2010 CS 225 1 Graphs Chapter 10

Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs

  • View
    222

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs

Spring 2010 CS 225 1

Graphs

Chapter 10

Page 2: Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs

Spring 2010 CS 225 2

Chapter Objectives• To become familiar with graph terminology and the

different types of graphs• To study a Graph ADT and different implementations

of the Graph ADT• To learn the breadth-first and depth-first search

traversal algorithms• To learn some algorithms involving weighted graphs• To study some applications of graphs and graph

algorithms

Page 3: Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs

Spring 2010 CS 225 3

More General Trees

Page 4: Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs

Spring 2010 CS 225 4

Other Linked Information

Page 5: Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs

Spring 2010 CS 225 5

The internet• http://isiosf.isi.it/

~jramasco/fig/wired.png

Page 6: Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs

Spring 2010 CS 225 6

Graph Terminology

• A graph is a data structure that consists of a set of vertices and a set of edges between pairs of vertices

• Edges represent paths or connections between the vertices

• The set of vertices and the set of edges must both be finite and neither one be empty

• A tree is a special case of a graph

Page 7: Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs

Spring 2010 CS 225 7

Visual Representation of Graphs• Vertices are

represented as points or labeled circles and edges are represented as lines joining the vertices

• The physical layout of the vertices and their labeling are not relevant

Page 8: Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs

Spring 2010 CS 225 8

Directed Graphs• The edges of a graph are directed if the

existence of an edge from A to B does not necessarily guarantee that there is a path in both directions

• A graph with directed edges is called a directed graph

Page 9: Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs

Spring 2010 CS 225 9

Graph Vocabulary

• A vertex is adjacent to another vertex if there is an edge to it from that other vertex

• A path is a sequence of vertices in which each successive vertex is adjacent to its predecessor

• In a simple path, the vertices and edges are distinct except that the first and last vertex may be the same

Page 10: Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs

Spring 2010 CS 225 10

Paths and Cycles

• A cycle is a simple path in which only the first and final vertices are the same

Page 11: Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs

Spring 2010 CS 225 11

Weighted Graphs

• The edges in a graph may have values associated with them known as their weights– Weight are usually assumed to be positive

• A graph with weighted edges is known as a weighted graph

Page 12: Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs

Spring 2010 CS 225 12

Connected vs. Unconnected• A graph is connected if there is a path between any

pair of vertices in the graph• If a graph is not connected, it may still consist of

connected endpoints

Page 13: Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs

Spring 2010 CS 225 13

The Graph ADT and Edge Class

• Java does not provide a Graph ADT• In making our own, we need to be able to do

the following• Create a graph with the specified number of vertices• Iterate through all of the vertices in the graph• Iterate through the vertices that are adjacent to a

specified vertex• Determine whether an edge exists between two vertices• Determine the weight of an edge between two vertices• Insert an edge into the graph

Page 14: Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs

Spring 2010 CS 225 14

The Graph ADT and Edge Class

Page 15: Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs

Spring 2010 CS 225 15

Implementing the Graph ADT• Because graph algorithms have been studied and

implemented throughout the history of computer science, many of the original publications of graph algorithms and their implementations did not use an object-oriented approach and did not even use abstract data types

• Two representations of graphs are most common– Edges are represented by an array of lists called adjacency

lists, where each list stores the vertices adjacent to a particular vertex

– Edges are represented by a two dimensional array, called an adjacency matrix

Page 16: Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs

Spring 2010 CS 225 16

Adjacency List

• An adjacency list representation of a graph uses an array of lists

• One list for each vertex

Page 17: Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs

Spring 2010 CS 225 17

Adjacency ListExample 1

Page 18: Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs

Spring 2010 CS 225 18

Adjacency List Example 2

Page 19: Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs

Spring 2010 CS 225 19

Adjacency Matrix

• Uses a two-dimensional array to represent a graph

• For an unweighted graph, the entries can be Boolean values

• For a weighted graph, the matrix would contain the weights

Page 20: Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs

Spring 2010 CS 225 20

Graph Class Hierarchy

Page 21: Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs

Spring 2010 CS 225 21

Class AbstractGraph

Page 22: Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs

Spring 2010 CS 225 22

The ListGraph Class

Page 23: Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs

Spring 2010 CS 225 23

Traversals of Graphs

• Most graph algorithms involve visiting each vertex in a systematic order

• Most common traversal algorithms are the breadth first and depth first search

Page 24: Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs

Spring 2010 CS 225 24

Breadth-First Search• In a breadth-first search, we visit the start

first, then all nodes that are adjacent to it next, then all nodes that can be reached by a path from the start node containing two edges, three edges, and so on– There is no special start vertex in a graph

• Must visit all nodes for which the shortest path from the start node is length k before we visit any node for which the shortest path from the start node is length k+1

Page 25: Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs

Spring 2010 CS 225 25

Algorithm for Breadth-First Search

Page 26: Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs

Spring 2010 CS 225 26

Example of a Breadth-First Search

Page 27: Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs

Spring 2010 CS 225 27

Trace of Breadth-First Search

Page 28: Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs

Spring 2010 CS 225 28

??? Breadth-First Search

Page 29: Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs

Spring 2010 CS 225 29

Depth-First Search

• In depth-first search, you start at a vertex, visit it, and choose one adjacent vertex to visit; then, choose a vertex adjacent to that vertex to visit, and so on until you go no further; then back up and see whether a new vertex can be found

Page 30: Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs

Spring 2010 CS 225 30

Depth-First Search Algorithm

Page 31: Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs

Spring 2010 CS 225 31

Depth-First Search

Page 32: Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs

Spring 2010 CS 225 32

Depth-First Search

Page 33: Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs

Spring 2010 CS 225 33

Implementing Depth-First Search

Page 34: Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs

Spring 2010 CS 225 34

Shortest Path Through a Maze

Page 35: Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs

Spring 2010 CS 225 35

Shortest Path Through a Maze

Page 36: Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs

Spring 2010 CS 225 36

Directed Acyclic Graph

• Directed graphs with no cycles are called directed acyclic graphs (DAG for short)

• They come up in a number of kinds of problems– scheduling– trees are DAGs

Page 37: Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs

Spring 2010 CS 225 37

DAG Examples

Page 38: Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs

Spring 2010 CS 225 38

Topological Sort of a Graph

• A topological sort of a DAG is an ordering of the vertices of the graph such that if (u, v) is an edge, u comes before v in the sort

• For the graph below– 0, 1, 2, 3, 4, 5, 6, 7, 8 is a valid topological sort– 0, 3, 1, 4, 6, 2, 5, 7, 8 is a valid topological sort– 0, 1, 5, 3, 4, 2, 6, 7, 8 is not a topologicl sort

Page 39: Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs

Spring 2010 CS 225 39

Topological Sort of a Graph

• If there is an edge (u, v) in a graph, the finish time of u must be after that of v in a depth-first search

• The the reverse of finish order of a depth-first search is a topological sort

• Algorithm for topological sort1. Perform a depth-first search

2. List the vertices in the reverse of their finish order

Page 40: Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs

Spring 2010 CS 225 40

Other Graph Algorithms

• Shortest Path for weighted graphs– Breadth-first search finds the shortest path if all

edges have the same weight

• Minimum spanning tree– Subset of the edges such that there is exactly one

path between any pair of vertices

Page 41: Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs

Spring 2010 CS 225 41

Shortest Path in Weighted Graph

• Finding the shortest path from a vertex to all other vertices

• Uses the following data– S is the set of vertices for which shortest distance

has been computer – V-S is the remaining vertices– d is an array containing the shortest distance from

s to each vertex– p is an array of predecessors for each vertex

Page 42: Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs

Spring 2010 CS 225 42

Shortest Path Algorithm

• Developed by Edsger Dijkstra

Page 43: Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs

Spring 2010 CS 225 43

Practice

Page 44: Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs

Spring 2010 CS 225 44

Spanning Tree• A minimum spanning tree is a subset of the

edges of a graph such that there is only one edge between each vertex, and all of the vertices are connected

• The cost of a spanning tree is the sum of the weights of the edges

• We want to find the minimum spanning tree or the spanning tree with the smallest cost– Solution formulated by R.C. Prim is very similar to

Dijkstra’s algorithm– Kruskal's algorithm is also commonly used

Page 45: Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs

Spring 2010 CS 225 45

Prim’s Algorithm

Page 46: Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs

Spring 2010 CS 225 46

Practice