Professional Documents
Culture Documents
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
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
Representations (Di-Graph)
Adjacency List Adjacency Matrix
Graph Operations
Insert Delete Search (Traversal)
Breadth-First Search Depth-First Search
Tree Traversals
Depth-First
In-order Preorder Post-order
Breadth-First
10
11
12
13
DONE!
14
15
C G
C G
16
17
BFS: An Example
C A D B E
The Queue
F H I
18
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
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
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
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
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
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
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
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
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
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
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
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
BFS: An Example
C A D B E G
DONE !
F H I
31
32
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
Find Cycles Euler Path and Circuit Problem Hamiltonian Path and Circuit Problem (or TSP)
33
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