39
GRAPHS

GRAPHS. Definition zThe Graph Data Structure Õset V of vertices Õcollection E of edges (pairs of vertices in V) zDrawing of a Graph Õvertex circle/oval

Embed Size (px)

Citation preview

Page 1: GRAPHS. Definition zThe Graph Data Structure Õset V of vertices Õcollection E of edges (pairs of vertices in V) zDrawing of a Graph Õvertex circle/oval

GRAPHS

Page 2: GRAPHS. Definition zThe Graph Data Structure Õset V of vertices Õcollection E of edges (pairs of vertices in V) zDrawing of a Graph Õvertex circle/oval

Definition

The Graph Data Structureset V of verticescollection E of edges (pairs of vertices in

V)

Drawing of a Graphvertex <-> circle/ovaledge <-> line connecting the vertex

pair

Page 3: GRAPHS. Definition zThe Graph Data Structure Õset V of vertices Õcollection E of edges (pairs of vertices in V) zDrawing of a Graph Õvertex circle/oval

Sample Uses

Airports and flightsPersons and acquaintance

relationshipsIntersections and streetsComputers and connections; the

Internet

Page 4: GRAPHS. Definition zThe Graph Data Structure Õset V of vertices Õcollection E of edges (pairs of vertices in V) zDrawing of a Graph Õvertex circle/oval

Graph Example

Graph G=(V,E):V={a,b,c,d}, E={(a,b),(b,c),(b,d),(a,d)}

d

ab

c

Page 5: GRAPHS. Definition zThe Graph Data Structure Õset V of vertices Õcollection E of edges (pairs of vertices in V) zDrawing of a Graph Õvertex circle/oval

Adjacency

Vertices connected by an edge are adjacent

An edge e is incident on a vertex v (and vice-versa) if v is an endpoint of e

Degree of a vertex v, deg(v)number of edges incident on vif directed graph, indeg(v) and outdeg(v)

Page 6: GRAPHS. Definition zThe Graph Data Structure Õset V of vertices Õcollection E of edges (pairs of vertices in V) zDrawing of a Graph Õvertex circle/oval

Graph Properties

Graph G with n vertices and m edges

v in G deg(v) = 2m

v in G indeg(v) = v in G outdeg(v) = mm is O(n2)

m <= n(n-1)/2 if G is undirectedm <= n(n-1) if G is directed

Page 7: GRAPHS. Definition zThe Graph Data Structure Õset V of vertices Õcollection E of edges (pairs of vertices in V) zDrawing of a Graph Õvertex circle/oval

Subgraphs

Graph H a subgraph of graph Gvertex set of H is a subset of vertex set of

Gedge set of H is a subset of edge set of G

Spanning subgraphvertex set of H identical to vertex set of G

Page 8: GRAPHS. Definition zThe Graph Data Structure Õset V of vertices Õcollection E of edges (pairs of vertices in V) zDrawing of a Graph Õvertex circle/oval

Paths and Cycles

(simple) Pathsequence of distinct vertices such that

consecutive vertices are connected through edges

(simple) Cyclesame as path except that first and last

vertices are identicalDirected paths and cycles

edge directions matter

Page 9: GRAPHS. Definition zThe Graph Data Structure Õset V of vertices Õcollection E of edges (pairs of vertices in V) zDrawing of a Graph Õvertex circle/oval

Connectivity

Connected graphthere is a path between any two

vertices

Page 10: GRAPHS. Definition zThe Graph Data Structure Õset V of vertices Õcollection E of edges (pairs of vertices in V) zDrawing of a Graph Õvertex circle/oval

Graph Methods

Container methodsGeneral or Global methods

numVertices(),numEdges(),vertices(),edges()Directed Edge methods

indegree(v), inadjacentVertices(v), inincidentEdges(v)

Update methodsadding/removing vertices or edges

Page 11: GRAPHS. Definition zThe Graph Data Structure Õset V of vertices Õcollection E of edges (pairs of vertices in V) zDrawing of a Graph Õvertex circle/oval

General Methods

numvertices()-returns the number of vertices in G

numedges()- returns the number of edges in G

vertices()- return an enumeration of the vertex positions in G

edges()- returns an enumeration of the edge positions in G

Page 12: GRAPHS. Definition zThe Graph Data Structure Õset V of vertices Õcollection E of edges (pairs of vertices in V) zDrawing of a Graph Õvertex circle/oval

General Methods

Degree (v) - returns the degree of v

adjacentvertices(v)- returns an enumeration of the vertices adjacent to v

incidentedges(v)- returns an enumeration of the edges incident upon v

endvertices(e)- return an array of size 2 storing the end vertices of e

Page 13: GRAPHS. Definition zThe Graph Data Structure Õset V of vertices Õcollection E of edges (pairs of vertices in V) zDrawing of a Graph Õvertex circle/oval

Directed Edges

Directededges()- return an enumeration of all directed edges

indegree(v)- return the indegree of v

outdegree(v)- return the outdegree of v

origin (v)- return the origin of directed edge e

other functions- inadjacentvertices(), outadjacentvertices(), destination()

Page 14: GRAPHS. Definition zThe Graph Data Structure Õset V of vertices Õcollection E of edges (pairs of vertices in V) zDrawing of a Graph Õvertex circle/oval

Updating Graphs

Insertedge(v,w,o)- insert and return an undirected edge between vertices v and w storing the object o at this position

Insertvertex(o)- insert and return a new vertex storing the object o at this position

removevertex (v)- remove the vertex v and all incident edges

removeedge(e)- remove edge e

Page 15: GRAPHS. Definition zThe Graph Data Structure Õset V of vertices Õcollection E of edges (pairs of vertices in V) zDrawing of a Graph Õvertex circle/oval

Implementation of a graph data structure

Page 16: GRAPHS. Definition zThe Graph Data Structure Õset V of vertices Õcollection E of edges (pairs of vertices in V) zDrawing of a Graph Õvertex circle/oval

Graph Implementations

Edge Listvertices and edges are nodesedge nodes refer to incident vertex nodes

Adjacency Listsame as edge list but vertex nodes also

refer to incident edge nodesAdjacency Matrix

2D matrix of vertices; entries are edges

Page 17: GRAPHS. Definition zThe Graph Data Structure Õset V of vertices Õcollection E of edges (pairs of vertices in V) zDrawing of a Graph Õvertex circle/oval

Implementation Examples

Illustrate the following graph using the different implementations

d

ab

c

1

2

3

4

5

Page 18: GRAPHS. Definition zThe Graph Data Structure Õset V of vertices Õcollection E of edges (pairs of vertices in V) zDrawing of a Graph Õvertex circle/oval

Edge List

1 542 3

A B DC

Page 19: GRAPHS. Definition zThe Graph Data Structure Õset V of vertices Õcollection E of edges (pairs of vertices in V) zDrawing of a Graph Õvertex circle/oval

Adjacency List

1 542 3

A B DC

in out outoutoutin in in

1 45

4 3 2 1 53

2

Page 20: GRAPHS. Definition zThe Graph Data Structure Õset V of vertices Õcollection E of edges (pairs of vertices in V) zDrawing of a Graph Õvertex circle/oval

Adjacency Matrix

a b c d

a

b

c

d

0

0

0

0

0 0

01

0

0

1

1 1

0

1

0

Page 21: GRAPHS. Definition zThe Graph Data Structure Õset V of vertices Õcollection E of edges (pairs of vertices in V) zDrawing of a Graph Õvertex circle/oval

Implementation Examples-Query

Illustrate the following graph using the different implementations

d

ab

c

1

2

3

4

5 e

6

8

7

Page 22: GRAPHS. Definition zThe Graph Data Structure Õset V of vertices Õcollection E of edges (pairs of vertices in V) zDrawing of a Graph Õvertex circle/oval

Comparing Implementations

Edge Listsimplest but most inefficient

Adjacency Listmany vertex operations are O(d) time

where d is the degree of the vertexAdjacency Matrix

adjacency test is O(1)addition/removal of a vertex: resize 2D

array

Page 23: GRAPHS. Definition zThe Graph Data Structure Õset V of vertices Õcollection E of edges (pairs of vertices in V) zDrawing of a Graph Õvertex circle/oval

Graph Traversals

Traversal- A systematic procedure for exploring a graph by examining all of its vertices and edges

Consistency- rules to determine the order of visits (top-down, alphabetical,etc.)- make the path consistent with the actual graph (don’t change conventions)

Page 24: GRAPHS. Definition zThe Graph Data Structure Õset V of vertices Õcollection E of edges (pairs of vertices in V) zDrawing of a Graph Õvertex circle/oval

Graph Traversals

Depth First Search (DFS)visit a starting vertex srecursively visit unvisited adjacent

verticesBreadth First Search (BFS)

visit starting vertex svisit unvisited vertices adjacent to s, then

visit unvisited vertices adjacent to them, and so on … (visit by levels)

Page 25: GRAPHS. Definition zThe Graph Data Structure Õset V of vertices Õcollection E of edges (pairs of vertices in V) zDrawing of a Graph Õvertex circle/oval

DFS Traversal

Depth First Search- recursively visit (traverse) unvisited vertices

Dead End- reaching a vertex where all the incident edges go to a previously visited vertex

Back Track- If a dead-end vertex is reached, go to the previous vertex

Page 26: GRAPHS. Definition zThe Graph Data Structure Õset V of vertices Õcollection E of edges (pairs of vertices in V) zDrawing of a Graph Õvertex circle/oval

Traversal Examples

DFS: a,b,c,d,h,e,f,g,l,k,j,i

f

a

b

e

d

h

c

g

ij k l

Page 27: GRAPHS. Definition zThe Graph Data Structure Õset V of vertices Õcollection E of edges (pairs of vertices in V) zDrawing of a Graph Õvertex circle/oval

DFS Traversal

Path of the DFS traversal- path=a,b,c,d,h- h is a dead end vertex- back track to d,c,b,a and go to another adjacent (unvisited) vertex -> e- path=a,e,f,g,k,j,i- i is a dead end vertex - back track to all the way back to a (also a dead end vertex)

finished

Page 28: GRAPHS. Definition zThe Graph Data Structure Õset V of vertices Õcollection E of edges (pairs of vertices in V) zDrawing of a Graph Õvertex circle/oval

BFS Traversal

Visit adjacent vertices by levelsdiscovery edges

- edges that lead to an unvisited vertex

cross edge- edges that lead to a previously visited vertex

Page 29: GRAPHS. Definition zThe Graph Data Structure Õset V of vertices Õcollection E of edges (pairs of vertices in V) zDrawing of a Graph Õvertex circle/oval

Traversal Examples

BFS: a,b,e,i,c,f,j,d,g,k,h,l

f

a

b

e

d

h

c

g

ij k l

Page 30: GRAPHS. Definition zThe Graph Data Structure Õset V of vertices Õcollection E of edges (pairs of vertices in V) zDrawing of a Graph Õvertex circle/oval

DFS Algorithm

Algorithm DFS(v)Input: Vertex vOutput: Depends on the application

mark v as visited; perform processingfor each vertex w adjacent to v do if w is unmarked then DFS(w)

Page 31: GRAPHS. Definition zThe Graph Data Structure Õset V of vertices Õcollection E of edges (pairs of vertices in V) zDrawing of a Graph Õvertex circle/oval

BFS Algorithm

Algorithm BFS(s)Input: Starting vertex sOutput: Depends on the application

// traversal algorithm uses a queue Q// loop terminates when Q is empty

Page 32: GRAPHS. Definition zThe Graph Data Structure Õset V of vertices Õcollection E of edges (pairs of vertices in V) zDrawing of a Graph Õvertex circle/oval

BFS Algorithm continued

Q new Queue()mark s as visited and Q.enqueue(s)while not Q.isEmpty() v Q.dequeue() perform processing on v for each vertex w adjacent to v do if w is unmarked then mark w as visited and Q.enqueue(w)

Page 33: GRAPHS. Definition zThe Graph Data Structure Õset V of vertices Õcollection E of edges (pairs of vertices in V) zDrawing of a Graph Õvertex circle/oval

Query

Let G be a graph whose vertices are the integers 1 through 8 and let the adjacent vertices of each vertex be given by the table below :Vertex Adjacent Vertices1 (2,3,4)2 (1,3,4)3 (1,2,4)4 (1,2,3,6)5 (6,7,8)6 (4,5,7)7 (5,6,8)8 (5,7)

Page 34: GRAPHS. Definition zThe Graph Data Structure Õset V of vertices Õcollection E of edges (pairs of vertices in V) zDrawing of a Graph Õvertex circle/oval

Query

Assume that in the traversal of G, the adjacent vertices of a given vertex are returned in the same order as they are listed in the above table:

a) Draw Gb) Give the sequence of vertices of G visited

using a DFS traversal order starting at vertex 1

c) Give the sequence of vertices visited using a BFS traversal starting at vertex 1

Page 35: GRAPHS. Definition zThe Graph Data Structure Õset V of vertices Õcollection E of edges (pairs of vertices in V) zDrawing of a Graph Õvertex circle/oval

Weighted GraphsOverview

Page 36: GRAPHS. Definition zThe Graph Data Structure Õset V of vertices Õcollection E of edges (pairs of vertices in V) zDrawing of a Graph Õvertex circle/oval

Weighted Graphs

Graph G = (V,E) such that there are weights/costs associated with each edgew((a,b)): cost of edge (a,b)representation: matrix entries <-> costs

d

ab

c

20

3265

12

e35

Page 37: GRAPHS. Definition zThe Graph Data Structure Õset V of vertices Õcollection E of edges (pairs of vertices in V) zDrawing of a Graph Õvertex circle/oval

Problems on Weighted Graphs

Minimum-cost Spanning TreeGiven a weighted graph G, determine a

spanning tree with minimum total edge cost

Single-source shortest pathsGiven a weighted graph G and a source

vertex v in G, determine the shortest paths from v to all other vertices in G

Path length: sum of all edges in path

Page 38: GRAPHS. Definition zThe Graph Data Structure Õset V of vertices Õcollection E of edges (pairs of vertices in V) zDrawing of a Graph Õvertex circle/oval

Weighted Graphs-Concerns

Minimum CostShortest Path

Page 39: GRAPHS. Definition zThe Graph Data Structure Õset V of vertices Õcollection E of edges (pairs of vertices in V) zDrawing of a Graph Õvertex circle/oval

Weighted Graphs

Shortest Path- Dijkstra’s Algorithm

Minimum Spanning Trees- Kruskal’s Algorithm