Graphs Chapter 12. Chapter 12: Graphs2 Graphs A graph is a data structure that consists of a set of...

27
Graphs Chapter 12

Transcript of Graphs Chapter 12. Chapter 12: Graphs2 Graphs A graph is a data structure that consists of a set of...

Page 1: Graphs Chapter 12. Chapter 12: Graphs2 Graphs A graph is a data structure that consists of a set of vertices and a set of edges between pairs of vertices.

Graphs

Chapter 12

Page 2: Graphs Chapter 12. Chapter 12: Graphs2 Graphs A graph is a data structure that consists of a set of vertices and a set of edges between pairs of vertices.

Chapter 12: Graphs 2

Graphs

• 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• Example:

• A vertex represents an airport • An edge represents a flight route between two airports and

stores the mileage of the route

ORD PVD

MIADFW

SFO

LAX

LGA

HNL

849

802

13871743

1843

10991120

1233337

2555

142

Page 3: Graphs Chapter 12. Chapter 12: Graphs2 Graphs A graph is a data structure that consists of a set of vertices and a set of edges between pairs of vertices.

Chapter 12: Graphs 3

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 4: Graphs Chapter 12. Chapter 12: Graphs2 Graphs A graph is a data structure that consists of a set of vertices and a set of edges between pairs of vertices.

Chapter 12: Graphs 4

Graphs

• A graph is a pair (V, E), where• V is a set of vertices• E is a set of pairs of vertices, called edges

• Example:• V = {A, B, C, D, E}• E = {(A,B), (A,D), (C,E), (D, E)}

Page 5: Graphs Chapter 12. Chapter 12: Graphs2 Graphs A graph is a data structure that consists of a set of vertices and a set of edges between pairs of vertices.

Chapter 12: Graphs 5

Edge Types

• Directed edge• ordered pair of vertices (u,v)• first vertex u is the origin• second vertex v is the destination• e.g., a flight

• Undirected edge• unordered pair of vertices (u,v)• e.g., a flight route

ORD PVDflight

AA 1206

ORD PVD849

miles

Page 6: Graphs Chapter 12. Chapter 12: Graphs2 Graphs A graph is a data structure that consists of a set of vertices and a set of edges between pairs of vertices.

Chapter 12: Graphs 6

Directed and Undirected Graphs

• A graph with directed edges is called a directed graph or digraph• Example: flight network

• A graph with undirected edges is an undirected graph or simply a graph• Example: route network

Page 7: Graphs Chapter 12. Chapter 12: Graphs2 Graphs A graph is a data structure that consists of a set of vertices and a set of edges between pairs of vertices.

Chapter 12: Graphs 7

Weighted Graphs

• The edges in a graph may have values associated with them known as their weights

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

Page 8: Graphs Chapter 12. Chapter 12: Graphs2 Graphs A graph is a data structure that consists of a set of vertices and a set of edges between pairs of vertices.

Chapter 12: Graphs 8

Terminology

• End vertices (or endpoints) of an edge• U and V are the endpoints of

a• Edges incident on a vertex

• a, d, and b are incident on V• Adjacent vertices

• U and V are adjacent• Degree of a vertex

• X has degree 5 • Parallel edges

• h and i are parallel edges• Self-loop

• j is a self-loop

XU

V

W

Z

Y

a

c

b

e

d

f

g

h

i

j

Page 9: Graphs Chapter 12. Chapter 12: Graphs2 Graphs A graph is a data structure that consists of a set of vertices and a set of edges between pairs of vertices.

Chapter 12: Graphs 9

P1

Terminology (cont.)

• 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

• Examples

• P1=(V,X,Z) is a simple path

• P2=(U,W,X,Y,W,V) is a path that is not simple

XU

V

W

Z

Y

a

c

b

e

d

f

g

hP2

Page 10: Graphs Chapter 12. Chapter 12: Graphs2 Graphs A graph is a data structure that consists of a set of vertices and a set of edges between pairs of vertices.

Chapter 12: Graphs 10

Terminology (cont.)

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

• Simple cycle• cycle such that all its

vertices and edges are distinct

• Examples

• C1=(V,X,Y,W,U,V) is a simple cycle

• C2=(U,W,X,Y,W,V,U) is a cycle that is not simple

C1

XU

V

W

Z

Y

a

c

b

e

d

f

g

hC2

Page 11: Graphs Chapter 12. Chapter 12: Graphs2 Graphs A graph is a data structure that consists of a set of vertices and a set of edges between pairs of vertices.

Chapter 12: Graphs 11

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 of G• A spanning subgraph of G is a

subgraph that contains all the vertices of G

Subgraph

Spanning subgraph

Page 12: Graphs Chapter 12. Chapter 12: Graphs2 Graphs A graph is a data structure that consists of a set of vertices and a set of edges between pairs of vertices.

Chapter 12: Graphs 12

Connectivity

• A graph is connected if there is a path between every pair of vertices

• A connected component of a graph G is a maximal connected subgraph of G Connected graph

Non connected graph with two connected components

Page 13: Graphs Chapter 12. Chapter 12: Graphs2 Graphs A graph is a data structure that consists of a set of vertices and a set of edges between pairs of vertices.

Chapter 12: Graphs 13

Trees and Forests

• A (free) tree is an undirected graph T such that• T is connected• T has no cycles

• A forest is an undirected graph without cycles

• The connected components of a forest are trees

Tree

Forest

Page 14: Graphs Chapter 12. Chapter 12: Graphs2 Graphs A graph is a data structure that consists of a set of vertices and a set of edges between pairs of vertices.

Chapter 12: Graphs 14

Spanning Trees and Forests

• A spanning tree of a connected graph is a spanning subgraph that is a tree

• A spanning tree is not unique unless the graph is a tree

• A spanning forest of a graph is a spanning subgraph that is a forest

Graph

Spanning tree

Page 15: Graphs Chapter 12. Chapter 12: Graphs2 Graphs A graph is a data structure that consists of a set of vertices and a set of edges between pairs of vertices.

Chapter 12: Graphs 15

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 16: Graphs Chapter 12. Chapter 12: Graphs2 Graphs A graph is a data structure that consists of a set of vertices and a set of edges between pairs of vertices.

Chapter 12: Graphs 16

Vertices and edges

• Represent the vertices by integers from 0 up to |V|-1, where |V| represents the cardinality of V.

• Define an Edge class containing• Source vertex• Destination vertex• Weight

• An Edge object represents a directed edge• For undirected edges, use two Edge objects, one in

each direction

Page 17: Graphs Chapter 12. Chapter 12: Graphs2 Graphs A graph is a data structure that consists of a set of vertices and a set of edges between pairs of vertices.

Chapter 12: Graphs 17

Edge Class

Page 18: Graphs Chapter 12. Chapter 12: Graphs2 Graphs A graph is a data structure that consists of a set of vertices and a set of edges between pairs of vertices.

Chapter 12: Graphs 18

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 19: Graphs Chapter 12. Chapter 12: Graphs2 Graphs A graph is a data structure that consists of a set of vertices and a set of edges between pairs of vertices.

Chapter 12: Graphs 19

Adjacency List

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

• One list for each vertex

Page 20: Graphs Chapter 12. Chapter 12: Graphs2 Graphs A graph is a data structure that consists of a set of vertices and a set of edges between pairs of vertices.

Chapter 12: Graphs 20

Adjacency List

• An array (Adj) of lists, one list per vertex• For each vertex u in V,

• Adj[u] contains all edges incident on u• Space required Θ(n+m), where n denotes the number of

vertices, and m denotes the number of edges

25

1

4 3

52

51 4

4

2 3 5

4 1 2

1

2

3

4

5

Page 21: Graphs Chapter 12. Chapter 12: Graphs2 Graphs A graph is a data structure that consists of a set of vertices and a set of edges between pairs of vertices.

Chapter 12: Graphs 21

Adjacency List (continued)

Page 22: Graphs Chapter 12. Chapter 12: Graphs2 Graphs A graph is a data structure that consists of a set of vertices and a set of edges between pairs of vertices.

Chapter 12: Graphs 22

Adjacency Matrix

• Uses a two-dimensional array to represent a graph• For an unweighted graph, the entries can be boolean values

• Or use 1 for the presence of an edge• For a weighted graph, the matrix would contain the weights

25

1

4 3

0 1 0 0 1

1 0 0 1 1

0 0 0 1 0

0 1 1 0 1

1 1 0 1 0

1

2

3

4

5

1 2 3 4 5

Page 23: Graphs Chapter 12. Chapter 12: Graphs2 Graphs A graph is a data structure that consists of a set of vertices and a set of edges between pairs of vertices.

Chapter 12: Graphs 23

Adjacency Matrix

Page 24: Graphs Chapter 12. Chapter 12: Graphs2 Graphs A graph is a data structure that consists of a set of vertices and a set of edges between pairs of vertices.

Chapter 12: Graphs 24

Overview of the Graph Class Hierarchy

Page 25: Graphs Chapter 12. Chapter 12: Graphs2 Graphs A graph is a data structure that consists of a set of vertices and a set of edges between pairs of vertices.

Chapter 12: Graphs 25

Class AbstractGraph

Page 26: Graphs Chapter 12. Chapter 12: Graphs2 Graphs A graph is a data structure that consists of a set of vertices and a set of edges between pairs of vertices.

Chapter 12: Graphs 26

The ListGraph Class

Page 27: Graphs Chapter 12. Chapter 12: Graphs2 Graphs A graph is a data structure that consists of a set of vertices and a set of edges between pairs of vertices.

Chapter 12: Graphs 27

Performance

• n vertices, m edges• no parallel edges• no self-loops• Bounds are “big-Oh”

AdjacencyList

Adjacency Matrix

Space n m n2

Iterate over incident edges of v

deg(v) n

isEdge (v, w) min(deg(v), deg(w)) 1

insert(v, w, o) 1 1