– Graphs 1 Graph Categories Strong Components Example of Digraph

38
1 Main Index Conten ts 1 Main Index Conten ts Graph Categories Example of Digraph Connectedness of Digrap h Adjacency Matrix Adjacency Set vertexInfo Object Vertex Map and Vector vInfo VtxMap and Vinfo Example Breadth-First Search Al gorithm (2 slides) Graphs Graphs Strong Components Graph G and Its Transpo se G T Shortest-Path Example ( 2 slides) Dijkstra Minimum-Path Algori thm (2 slides) Minimum Spanning Tree E xample Minimum Spanning Tree : vertices A and B Completing the Minimum Spanning-Tree with Vertices C and

description

Königsberg Bridge Problem Land River Land A and D are two islands in the river; a, b, c, d, e, f, and g are bridges. Question: Starting at one land area, is it possible to walk across all the bridges exactly once and return to the starting area?

Transcript of – Graphs 1 Graph Categories Strong Components Example of Digraph

Page 1: – Graphs 1 Graph Categories Strong Components Example of Digraph

1 Main Index Contents1 Main Index Contents

Graph Categories

Example of Digraph

Connectedness of Digraph

Adjacency Matrix

Adjacency Set

vertexInfo Object

Vertex Map and Vector vInfo

VtxMap and Vinfo Example

Breadth-First Search Algorithm (2 slides)

Dfs()

– – GraphsGraphsStrong Components

Graph G and Its Transpose GT

Shortest-Path Example (2 slides)

Dijkstra Minimum-Path Algorithm (2 slides)

Minimum Spanning Tree Example

Minimum Spanning Tree: vertices A and B

Completing the Minimum Spanning-Tree

with Vertices C and D

Summary Slides (4 slides)

Page 2: – Graphs 1 Graph Categories Strong Components Example of Digraph

2 Main Index Contents

Königsberg Bridge ProblemKönigsberg Bridge Problem

AD

a

b

cd

e

f

g

B

C

River

Land

Land

A and D are two islands in the river;

a, b, c, d, e, f, and g are bridges.

Question: Starting at one land area, is it possible to walk across all the bridges exactly once and return to the starting area?

Page 3: – Graphs 1 Graph Categories Strong Components Example of Digraph

3 Main Index Contents

Euler’s ModelEuler’s Model

C

B

A D

c

a

d

b

e

f

g

Page 4: – Graphs 1 Graph Categories Strong Components Example of Digraph

4 Main Index Contents

Graph definitions and notationsGraph definitions and notations A graph G is a pair, G = (V, E), where V is a finite nonempty set of

vertices of G and E V x V., i.e., the elements of E are pair of elements of V and E is called edges.

Let V(G) denote the set of vertices, and E(G) denote the set of edges of G.

– if elements of E(G) are ordered pairs, G is called directed graph or digraph; otherwise, G is called an undirected graph in which (u, v) and (v, u) represent the same edge.

Let G be a graph, a graph H is called a subgraph of G if V(H) V(G) and E(H) E(G); i.e., every vertex of H is a vertex of G and every edge in H is an edge in G.

A graph can be shown pictorially: a vertex is drawn as a circle, and an edge is drawn as a line. In a directed graph, the edges are drawn using arrows. Additional terms will be defined in the following slides.

Page 5: – Graphs 1 Graph Categories Strong Components Example of Digraph

5 Main Index Contents5 Main Index Contents

Graph CategoriesGraph Categories

( a : C o n n e c t e d) ( c : C o m p le t e )( b: D isc o n n e c t e d)

A graph is connected if each pair of vertices have a edge or path between them

A complete graph is a connected graph in which each pair of vertices are linked by an edge

Page 6: – Graphs 1 Graph Categories Strong Components Example of Digraph

6 Main Index Contents6 Main Index Contents

Example of DigraphExample of Digraph

Vert ices V = { A , B , C , D , E }E d ges E = { (A ,B ), (A ,C ), (A ,D ), (B ,D ), (B ,E ), (C ,A ), (D ,E )}

A

DE

B

C

Graph with ordered edges are called directed graphs or digraphs

Page 7: – Graphs 1 Graph Categories Strong Components Example of Digraph

7 Main Index Contents7 Main Index Contents

Connectedness of DigraphConnectedness of Digraph

A

C

B

E

D

A

DC

B

C

A

ED

B

(a) (b ) (c)

N o t St ro ngly o r W eak ly C on nect ed(N o p at h E t o D o r D t o E )

St ron gly C o nn ect ed W eak ly C o n n ect ed(N o p at h fro m D t o a v ert ex)

Strongly connected if there is a path from any vertex to any other vertex. Weakly connected if, for each pair of vertices vi and vj,

there is either a path P(vi, vj) or a path P(vi,vj).

Page 8: – Graphs 1 Graph Categories Strong Components Example of Digraph

8 Main Index Contents8 Main Index Contents

Representation of a graph: Representation of a graph: Adjacency MatrixAdjacency Matrix

( a)

D

A

C

E

B

( b )D

B

A

E

C

4

2 7

3

6

4

1 2

An m by m matrix (where m is the number of vertices in the graph), called an adjacency matrix, identifies the edges. An entry in row i and column j corresponds to the edge e = (vi,, vj). Its value is the weight of the edge, or –1 (or 0) if the edge does not exist.

Page 9: – Graphs 1 Graph Categories Strong Components Example of Digraph

9 Main Index Contents9 Main Index Contents

Another way of Representing a Another way of Representing a graph: Adjacency listgraph: Adjacency list

( a )

D

A

C

E

B

A

E

D

C

B

Vert ices Set o f N eighb o rs

B 1 C 1

C 1

B 1

D 1B 1

A

E

D

C

B

Vert ices Set o f N eigh b ors(b )

D

B

A

E

C

4

2 7

3

6

4

1 2

C 1

C 7B 4

A 2

B 3

E 4

E 2

D 6

Page 10: – Graphs 1 Graph Categories Strong Components Example of Digraph

10 Main Index Contents

Definition of a node in a graphDefinition of a node in a graphtemplate <class vType)class nodeType {public:

vType vertex;nodeType<vType> *link;

};

Page 11: – Graphs 1 Graph Categories Strong Components Example of Digraph

11 Main Index Contents

Operations on a GraphOperations on a Graph Common operations

– Create a graph (demonstration based on adjacent list with a linked list)

– Clear the graph– print a graph– Determine if the graph is empty– Traverse the graph– Others (depending on applications)

Page 12: – Graphs 1 Graph Categories Strong Components Example of Digraph

12 Main Index Contents

Review: Linked list ADTReview: Linked list ADTtemplate <class Type>class nodeType {public:

type info;nodetype<Type> *link;

};Template<class Type>class linkedListType {public:

const linkedListtype<Type> &operator=(const linkedListtype<Type> &);void initializedList( );bool isEmpty( );void print( );int length( );void destroyList( );

Page 13: – Graphs 1 Graph Categories Strong Components Example of Digraph

13 Main Index Contents

Review: Linked list ADT Review: Linked list ADT continuedcontinued

void retrieveFirst(Type &firstElement);void search(const Type &searchItem); // displys “Item is/is not found”void insertFirst(const Types &newItem);void insertLast(const Types &newItem);void deleteNode(const types &deleteItem);linkedListType( ); // initialize first = last = NULLlinkedListType(const linkedListtype<type> &otherList); // copy constructor~linkedListType( ); // delete all nodes in the listprotedted:

nodeType<Type> *first;nodeType<Type> *last;

};

Page 14: – Graphs 1 Graph Categories Strong Components Example of Digraph

14 Main Index Contents

Review: TemplateReview: Template// specifies an array data member with generic element// type elemType and its size with the following templatetemplate<class elemType, int size>class listType {public:

…private:

int maxSize;int length;elemType listelem[size];

};

To create a list of 100 int elements:

listType<int, 100> inList;

Page 15: – Graphs 1 Graph Categories Strong Components Example of Digraph

15 Main Index Contents

Graph ADT: Adjacent list Graph ADT: Adjacent list approach – the interfaceapproach – the interface

template<class vType, int size>class graphType {public:

bool isEmpty( );void createGraph( );void clearGraph( ); // vertices deallocatedvoid printGraph( );graphtype( ); // default constructor; gsize = 0 and maxSize = size~graphType( ); // storage deallocated

protected:int maxSize; // max number of verticesint size; // current number of verticeslinkedListGraph<vType> *graph; // array of pointers to create// the adjacency list

};

Page 16: – Graphs 1 Graph Categories Strong Components Example of Digraph

16 Main Index Contents

Graph ADT: Adjacent list Graph ADT: Adjacent list approach – the implementationapproach – the implementation

template<class vType, int size>bool graphType<vtype, size>::isEmpty( ){

return (gsize == 0);}To implement the createGraph( ) funciton: assuming the user

is prompted to enter at run time 1) number of vertices, 2) each and every vertex and its adjacent vertices, e.g.,

50 2 4 … -991 0 3 … -992 …Note that –99 is the sentinel value used to terminal the input

loop.

Page 17: – Graphs 1 Graph Categories Strong Components Example of Digraph

17 Main Index Contents

Graph ADT: Adjacent list Graph ADT: Adjacent list approach – the implementationapproach – the implementation

template<class vType, int size>bool graphType<vtype, size>::createGraph( ){

vtype vertex, adjacentVertex;if (gSize != 0)

clearGraph( );cout << “Enter # vertices followed by each individual vertices and “

<< “its adjacent vertices ending with –99: ”;cin >> gSize;for (int i = 0; i < gSize; i++) {

cin >> vertex;cin >> adjacentVertex;while (adjacentVertex != -99) {

graph[vertex].insertLast(adjacentVertex);cin >> adjacentVertex;

}}

}

Page 18: – Graphs 1 Graph Categories Strong Components Example of Digraph

18 Main Index Contents

Graph ADT: Adjacent list Graph ADT: Adjacent list approach – the implementationapproach – the implementation

template<class vType, int size>bool graphType<vtype, size>::clearGraph( ){

int i;for (i = 0; i < gSize; i++)

graph[i].destroyList( );gSize = 0;

}

Page 19: – Graphs 1 Graph Categories Strong Components Example of Digraph

19 Main Index Contents

The following slides for The following slides for reference onlyreference only

Page 20: – Graphs 1 Graph Categories Strong Components Example of Digraph

20 Main Index Contents20 Main Index Contents

vertexInfo ObjectvertexInfo Object

v t x M a p L o c

e dge s ( se t o f n e igh bo r s)

in D e gr e e

o c c up ie d

c o lo r

Id en t ifies Vert ex

U s ed t o bu ildan d u s e a grap h

da t a Va lue

p a r e n t

v

v t xM ap L o c

Vert ex v in a m apv ert exIn fo O b ject

A d jacen cy Set

d es t w eig h t

d es t w eig h t

A vertexInfo object consists of seven data members. The first two members, called vtxMapLoc and edges, identify the vertex in the map and its adjacency set.

Page 21: – Graphs 1 Graph Categories Strong Components Example of Digraph

21 Main Index Contents21 Main Index Contents

Vertex Map and Vector vInfoVertex Map and Vector vInfo

v e r t e xm I t e r

( it e r a t o rlo c a t io n )

in de x

. . .in de x

v t x M a p L o c

e dge s

in D e gr e e

o c c up ie d

c o lo r

da t a Va lue

v t x M a p

dist a n c e

v In fo

To store the vertices in a graph, we provide a map<T,int> container, called vtxMap, where a vertex name is the key of type T. The int field of a map object is an index into a vector of vertexInfo objects, called vInfo. The size of the vector is initially the number of vertices in the graph, and there is a 1-1 correspondence between an entry in the map and a vertexInfo entry in the vector

Page 22: – Graphs 1 Graph Categories Strong Components Example of Digraph

22 Main Index Contents

VtxMap and Vinfo ExampleVtxMap and Vinfo Example

v t x M a p

A 0

lo c AB 1

lo c BC 2

lo c CD 3

lo c D0 21 3

v I n f o

1 ( B ) 1

3 ( D ) 1

0 ( A ) 12 ( C ) 1

2 ( C ) 1

lo c A

e dge s

in D e gr e e = 1

. . . .

lo c B

e dge s

in D e gr e e = 1

. . . .

lo c C

e dge s

in D e gr e e = 2

. . . .

lo c D

e dge s

in D e gr e e = 1

. . . .

Page 23: – Graphs 1 Graph Categories Strong Components Example of Digraph

23 Main Index Contents

Breadth-First Search AlgorithmBreadth-First Search Algorithm

E

D

A

CB

F

G

B

vis it Q u eu e

v is it Set A

(a)

B C G v is it Q u eu e

v is it Set A

(b )

C G D vis it Q u eu e

v is it Set A

(c)

G D

CB

Page 24: – Graphs 1 Graph Categories Strong Components Example of Digraph

24 Main Index Contents

Breadth-First Search… (Cont.)Breadth-First Search… (Cont.)

E Fv is it Q u eu e

v is it SetA

(e)

DG

CB

Dv is it Q u eu e

v is it SetA

(d )

G

CB

E

F

v is it Q u eu e

v is it SetA

(g)

DG

CB

Fv is it Q u eu e

v is it SetA

(f)

G

CB

D E

Page 25: – Graphs 1 Graph Categories Strong Components Example of Digraph

25 Main Index Contents

dfs()dfs()

AB

C

v = A , w = BF irs t call t o d fs Vis it () s t art s at D .Secon d call t o d fs Vis it () s t art s at A .d fs Lis t : A D C B

D

(a)

B

D

C

v S = A , v = B , w = Cd fs Vis it () s t art s at B , d is co v ersn eigh bo r D , an d t h en n eigh b o r C .d fs L is t : A B D C

(b )

A

DC

v S = A , v = B , w = Cdfs Vis it () s t art s at A , d is co vers C ,d is co v ers D , and finally d is co v ersB . T h e n eigh b or o f B (C ) is G R A Y(a b ack edge).d fs L is t : < em p t y >p at h C D B C is a cy cle

B

A C

B

v S = A , v = B , w = Cd fs Vis it () s t art s at A an d p ro ceed salon g t he p at h fro m B t o C .d fs L is t : A B C

(d )

A

(c)

Page 26: – Graphs 1 Graph Categories Strong Components Example of Digraph

26 Main Index Contents

Strong ComponentsStrong Components

F

E

C

A

BC o m p o n en tsA, B, CD , F , GED

G

A strongly connected component of a graph G is a maximal set of vertices SC in G that are mutually accessible.

Page 27: – Graphs 1 Graph Categories Strong Components Example of Digraph

27 Main Index Contents

Graph G and Its Transpose Graph G and Its Transpose GGTT

F

E

C

A

B

G

D

G rap h G

F

E

C

A

B

G

D

G rap h G T

The transpose has the same set of vertices V as graph G but a new edge set ET consisting of the edges of G but with the opposite direction.

Page 28: – Graphs 1 Graph Categories Strong Components Example of Digraph

28 Main Index Contents

Shortest-Path ExampleShortest-Path Example

A B

CD

E

F

The shortest-path algorithm includes a queue that indirectly stores the vertices, using the corresponding vInfo index. Each iterative step removes a vertex from the queue and searches its adjacency set to locate all of the unvisited neighbors and add them to the queue.

Page 29: – Graphs 1 Graph Categories Strong Components Example of Digraph

29 Main Index Contents29 Main Index Contents

Shortest-Path ExampleShortest-Path Example

E 1 , F A 2 , D

v is it Q u eu e

(a)

v is it Q u eu e

(c)

v is it Q u eu e

(b )

D 1 , F A 2 , DE 1 , F

E 1 , F A 2 , D

v is it Q ueu e

(a)

v is it Q ueu e

(c)

v is it Q ueu e

(b )

D 1 , F A 2 , DE 1 , F B 3 ,A C 3 ,A

v is it Q u eu e

(d )

Example: Find the shortest path for the previous graph from F to C.

Page 30: – Graphs 1 Graph Categories Strong Components Example of Digraph

30 Main Index Contents30 Main Index Contents

Dijkstra Minimum-Path Dijkstra Minimum-Path Algorithm From A to D ExampleAlgorithm From A to D Example

A B

CD

E

F

6

4

2 0 1 4

12

4

2

6

8 11

m in In fo (B ,4 ) m in In fo (E ,4 )m in In fo (C ,1 1 )

p rio rit y q u eu e

Page 31: – Graphs 1 Graph Categories Strong Components Example of Digraph

31 Main Index Contents

Dijkstra Minimum-Path Dijkstra Minimum-Path Algorithm From… (Cont…)Algorithm From… (Cont…)

m in In fo (C ,1 0 ) m in In fo (E ,4 )m in In fo (C ,1 1 )

p rio rit y q u eu e

m in In fo (D ,1 2 )

m in In fo (C ,1 0 ) m in In fo (C ,1 1 )

p rio rit y q u eu e

m in In fo (D ,1 2 )

p rio rit y q u eu e

m in In fo (D ,1 2 )

Page 32: – Graphs 1 Graph Categories Strong Components Example of Digraph

32 Main Index Contents32 Main Index Contents

Minimum Spanning Tree Minimum Spanning Tree ExampleExample

B

A

H

E

G

F

D

C

5 0 25

462 5

23

5 5

3 29 8

35

6 7

B

A

H

E

G

F

D

C

25

4 62 5

23

5 5

3 2

35

M in im u m am o u n t o f cab le = 2 41

M in im u m s p ann in g t reeN et w o rk o f H u b s

Page 33: – Graphs 1 Graph Categories Strong Components Example of Digraph

33 Main Index Contents33 Main Index Contents

Minimum Spanning Tree: Minimum Spanning Tree: Vertices A and BVertices A and B

A

B C

D

5

A

B

(a) (b )

Sp an n in g t ree w it h v ert ices A , Bm in Sp an T reeSiz e = 2 , m in T reeW eigh t = 2

Page 34: – Graphs 1 Graph Categories Strong Components Example of Digraph

34 Main Index Contents

Completing the Minimum Spanning-Tree Completing the Minimum Spanning-Tree Algorithm with Vertices C and DAlgorithm with Vertices C and D

C

7D

A

B

(a)

Sp ann in g t ree w it h v ert ices A , B , Dm in Sp an T reeSiz e = 3 , m in T reeW eigh t = 7

2

5

D

A

B

(b )

Sp ann in g t ree w it h v ert ices A , B , D , Cm in Sp an T reeSiz e = 4 , m in T reeW eigh t = 1 4

2

5

Page 35: – Graphs 1 Graph Categories Strong Components Example of Digraph

35 Main Index Contents35 Main Index Contents

Summary Slide 1Summary Slide 1

§- Undirected and Directed Graph (digraph) - Both types of graphs can be either weighted or

nonweighted.

Page 36: – Graphs 1 Graph Categories Strong Components Example of Digraph

36 Main Index Contents36 Main Index Contents

Summary Slide 2Summary Slide 2

§- Breadth-First, bfs() - locates all vertices reachable from a starting vertex

- can be used to find the minimum distance from a starting vertex to an ending vertex in a graph.

Page 37: – Graphs 1 Graph Categories Strong Components Example of Digraph

37 Main Index Contents37 Main Index Contents

Summary Slide 3Summary Slide 3

§- Depth-First search, dfs() - produces a list of all graph vertices in the reverse

order of their finishing times.

- supported by a recursive depth-first visit function, dfsVisit()

- an algorithm can check to see whether a graph is acyclic (has no cycles) and can perform a

topological sort of a directed acyclic graph (DAG)

- forms the basis for an efficient algorithm that finds the strong components of a graph

Page 38: – Graphs 1 Graph Categories Strong Components Example of Digraph

38 Main Index Contents38 Main Index Contents

Summary Slide 4Summary Slide 4

§- Dijkstra's algorithm - if weights, uses a priority queue to determine a path

from a starting to an ending vertex, of minimum weight

- This idea can be extended to Prim's algorithm, which computes the minimum spanning tree in an undirected, connected graph.