Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graphs Chapter 12.

Similar presentations


Presentation on theme: "Graphs Chapter 12."— Presentation transcript:

1 Graphs Chapter 12

2 Graphs PVD ORD SFO LGA HNL LAX DFW MIA
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 849 PVD 1843 ORD 142 SFO 802 LGA 1743 337 1387 HNL 2555 1099 LAX 1233 DFW 1120 MIA 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 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)} Chapter 12: Graphs

5 Edge Types flight AA 1206 ORD PVD 849 miles ORD PVD 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 flight AA 1206 ORD PVD 849 miles ORD PVD 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 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 Chapter 12: Graphs

8 Terminology X U V W Z Y a c b e d f g h i j
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 X U V W Z Y a c b e d f g h i j Chapter 12: Graphs

9 Terminology (cont.) V a b P1 d U X Z P2 h c e W g f Y
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 V a b P1 d U X Z P2 h c e W g f Y Chapter 12: Graphs

10 Terminology (cont.) V a b d U X Z C2 h e C1 c W g f Y
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 V a b d U X Z C2 h e C1 c W g f Y 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 Chapter 12: Graphs

12 Non connected graph with two connected components
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 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 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 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 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 Chapter 12: Graphs

17 Edge Class 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 Chapter 12: Graphs

19 Adjacency List An adjacency list representation of a graph uses an array of lists One list for each vertex Chapter 12: Graphs

20 Adjacency List 1 2 3 4 5 2 5 1 1 5 4 5 2 4 2 3 5 4 3 4 1 2 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 Chapter 12: Graphs

21 Adjacency List (continued)
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 1 1 2 3 4 5 1 5 2 4 3 Chapter 12: Graphs

23 Adjacency Matrix Chapter 12: Graphs

24 Overview of the Graph Class Hierarchy
Chapter 12: Graphs

25 Class AbstractGraph Chapter 12: Graphs

26 The ListGraph Class Chapter 12: Graphs

27 Performance Adjacency List Adjacency Matrix Space n + m n2
n vertices, m edges no parallel edges no self-loops Bounds are “big-Oh” Adjacency List 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) Chapter 12: Graphs


Download ppt "Graphs Chapter 12."

Similar presentations


Ads by Google