You are on page 1of 34

CS 306 DAA, IIITDM Jabalpur

Graph Data Structures and Algorithms


Atul Gupta

Color this map !!!

CS 306 DAA, IIITDM Jabalpur

Is the problem clear?


Too much of Information
intricate boundaries, border posts where three or more countries meet, open, seas, meandering rivers

leading to distraction !!!

CS 306 DAA, IIITDM Jabalpur

Graph Coloring
Not just the exclusive domain of map designers! For Instance: Time table scheduling in IIITDMJ
Suppose IIITJ needs to schedule its classes and wants to use the fewest time slots possible. The only constraint is that two classes cannot be scheduled concurrently if some student will be taking both of them. To express this problem as a graph, use one vertex for each class and put an edge between two vertices if there is a conflict, i.e., if there is somebody taking both endpoint classes. Think of each time slots as having its own color. Then, problem of assigning time slots to the classes is exactly the same as coloring this graph!

Graphs
Most un-restricted form of data organization Final destination for problem solving Many variances
Directed and un-directed graphs Connected and un-connected graphs Weighted graphs

CS 306 DAA, IIITDM Jabalpur

Some Definitions
An undirected graph G is a pair (V, E), where V is a finite set of points called vertices and E is a finite set of edges An edge e E is an unordered pair (u, v), where u, v V In a directed graph, the edge e is an ordered pair (u, v). An edge (u, v) is incident from vertex u and is incident to vertex v. A path from a vertex u to a vertex v is a sequence <v0, v1 , v2, , vk> of vertices where v0 = u, vk = v, and (vi, vi+1) E for i = 0, 1,, k-1 The length of a path is defined as the number of edges in the path.

Representations of a Graph
Undirected Graph
Adjacency List Adjacency Matrix

CS 306 DAA, IIITDM Jabalpur

Representations (Di-Graph)
Adjacency List Adjacency Matrix

Some more terms


An undirected graph is connected if every pair of vertices is connected by a path. A forest is an acyclic graph, and a tree is a connected acyclic graph. A graph that has weights associated with each edge is called a weighted graph.

CS 306 DAA, IIITDM Jabalpur

Graph Operations
Insert Delete Search (Traversal)
Breadth-First Search Depth-First Search

Tree Traversals
Depth-First
In-order Preorder Post-order

Breadth-First

CS 306 DAA, IIITDM Jabalpur

Tree reversal: BFS


A B D H E I F J K C G

Tree reversal: BFS - Implementation


Idea
Use of a queue data structure A node is said to be traversed when its all successor nodes are generated, and queued

Demo: Use of a coloring scheme


White: not accessed/not encountered/not generated Yellow: encountered but not traversed A A B C Red: traversed
D E F G

CS 306 DAA, IIITDM Jabalpur

Tree reversal: BFS - Implementation


A B D H E I F J K C G A

Tree reversal: BFS - Implementation


A B D H E I F J K C G B C

CS 306 DAA, IIITDM Jabalpur

Tree reversal: BFS - Implementation


A B D H E I F J K C G B C

Tree reversal: BFS - Implementation


A B D H E I F J K C G C D E

CS 306 DAA, IIITDM Jabalpur

Tree reversal: BFS - Implementation


A B D H E I F J K C G C D E

Tree reversal: BFS - Implementation


A B D H E I F J K C G D E F G

10

CS 306 DAA, IIITDM Jabalpur

Tree reversal: BFS - Implementation


A B D H E I F J K C G D E F G

Tree reversal: BFS - Implementation


A B D H E I F J K C G F G H

11

CS 306 DAA, IIITDM Jabalpur

Tree reversal: BFS - Implementation


A B D H E I F J K C G F G H

Tree reversal: BFS - Implementation


A B D H E I F J K C G G H I J

12

CS 306 DAA, IIITDM Jabalpur

Tree reversal: BFS - Implementation


A B D H E I F J K C G H I J

Tree reversal: BFS - Implementation


A B D H E I F J K C G I J

13

CS 306 DAA, IIITDM Jabalpur

Tree reversal: BFS - Implementation


A B D H E I F J K C G JK

Tree reversal: BFS - Implementation


A B D H E I F J K C G

DONE!

14

CS 306 DAA, IIITDM Jabalpur

Tree reversal: DFS


A B D H E I F J K C G

Tree reversal: DFS - Implementation


A B D H E I F J K C G

15

CS 306 DAA, IIITDM Jabalpur

Tree reversal: DFS - Implementation


A B D H E I F J K
A

C G

Tree reversal: DFS - Implementation


A B D H E I F J
C

C G

16

CS 306 DAA, IIITDM Jabalpur

Tree reversal: DFS - Implementation


A B D H E I F J K C G
G F B

Graph Search (Traversals)


BFS DFS

17

CS 306 DAA, IIITDM Jabalpur

Breadth-First Search (Traversal)


Idea
Identify the starting node (this will be given to you) Generate a traversal tree while traversing the graph A node is said to be traversed when its all successor nodes are generated

Implementation: Use of a coloring scheme


White: not accessed/not encountered/not generated Yellow: encountered but not traversed Red: traversed

BFS: An Example
C A D B E
The Queue

F H I

18

CS 306 DAA, IIITDM Jabalpur

BFS: An Example
C A D B E
The Queue

F H I

BFS: An Example
C A D B E G H I F

19

CS 306 DAA, IIITDM Jabalpur

BFS: An Example
C A D B E
B

F H I

BFS: An Example
C A D B E
B D

F H I

20

CS 306 DAA, IIITDM Jabalpur

BFS: An Example
C A D B E
B D C

F H I

BFS: An Example
C A D B E
B D C

F H I

21

CS 306 DAA, IIITDM Jabalpur

BFS: An Example
C A D B E
D C

F H I

BFS: An Example
C A D B E
D C E

F H I

22

CS 306 DAA, IIITDM Jabalpur

BFS: An Example
C A D B E
D C E

F H I

BFS: An Example
C A D B E
C E

F H I

23

CS 306 DAA, IIITDM Jabalpur

BFS: An Example
C A D B E
C E H

F H I

BFS: An Example
C A D B E
C E H

F H I

24

CS 306 DAA, IIITDM Jabalpur

BFS: An Example
C A D B E
E H

F H I

BFS: An Example
C A D B E
E H F

F H I

25

CS 306 DAA, IIITDM Jabalpur

BFS: An Example
C A D B E
E H F

F H I

BFS: An Example
C A D B E
H F

F H I

26

CS 306 DAA, IIITDM Jabalpur

BFS: An Example
C A D B E
H F G

F H I

BFS: An Example
C A D B E
H F G

F H I

27

CS 306 DAA, IIITDM Jabalpur

BFS: An Example
C A D B E
F G

F H I

BFS: An Example
C A D B E
F G I

F H I

28

CS 306 DAA, IIITDM Jabalpur

BFS: An Example
C A D B E
F G I

F H I

BFS: An Example
C A D B E
F G I

F H I

29

CS 306 DAA, IIITDM Jabalpur

BFS: An Example
C A D B E
G I

F H I

BFS: An Example
C A D B E
I

F H I

30

CS 306 DAA, IIITDM Jabalpur

BFS: An Example
C A D B E G
DONE !

F H I

Breadth-First Search (Traversal)


ALGORITHM
BFS(G, s) 1 for each vertex u V [G] do 2 color[u] WHITE 3 d[u] 4 [u] NIL 5 color[s] YELLOW 6 d[s] 0 7 [s] NIL 8 Q 9 ENQUEUE(Q, s) 10 while Q do 11 u DEQUEUE(Q) 12 for each v Adj[u] do 13 if color[v] = WHITE 14 then color[v] YELLOW 15 d[v] d[u] + 1 16 [v] u 17 ENQUEUE(Q, v) 18 doSomthing(u) 19 color[u] RED

31

CS 306 DAA, IIITDM Jabalpur

Graph: Depth-First Traversal


The Idea
Same as with tree traversal using DFS Use a stack Use three colors White, Yellow, and Red, similarly

Graph: Depth-First Traversal


Example C A D B E G H I F

32

CS 306 DAA, IIITDM Jabalpur

Depth-First Traversal
DFS(G) 1 for each vertex u V [G] do 2 color[u] WHITE 3 [u] NIL 4 me 0 5 for each vertex u V [G] do 6 if color[u] = WHITE then 7 DFS-VISIT(u) DFS-VISIT(u) 1 color[u] YELLOW 2 me me +1 3 d[u] me //discovery time 4 for each v Adj[u] do 5 if color[v] == WHITE 6 then [v] u 7 DFS-VISIT(v) 8 color[u] RED 9 f [u] me me +1 //finish time

Problems Specific with Graphs


Minimum Spanning Tree Path Problems
Simple Paths Shortest Path Problem
Single source shortest paths All-pair shortest paths

Find Cycles Euler Path and Circuit Problem Hamiltonian Path and Circuit Problem (or TSP)

Graph Coloring Connected Components Isomorphic graphs Search Graphs

33

CS 306 DAA, IIITDM Jabalpur

Graph Applications
Connection problems Scheduling problems Transportation problems Network analysis Games and Puzzles

Graphs: Summary
Two variants:
Directed and Undirected

Representation
Array and Link-List

Traversals
BFS DFS

Stacks

68

34

You might also like