Students can learn about graphs data structures. this PPT covers the following topics in GRAPHS data structures: graph representation, types of graphs, graph traversals like DFS and BFS, Topological Sort, Applications of DFS and BFS.
1. UNIT III : GRAPHS
By
Mr.S.Selvaraj
Asst. Professor (SRG) / CSE
Kongu Engineering College
Perundurai, Erode, Tamilnadu, India
Thanks to and Resource from : Data Structures and Algorithm Analysis in C by Mark Allen Weiss & Sumitabha Das, “Computer Fundamentals and C
Programming”, 1st Edition, McGraw Hill, 2018.
20CST32 – Data Structures
2. Syllabus – Unit Wise
4/6/2022
3.1 _ Graph Definition, Type and
Representation
2
4. Text Book and Reference Book
4/6/2022
3.1 _ Graph Definition, Type and
Representation
4
5. Unit III : Contents
1. Graph Definitions
2. Types of Graph
3. Representation of Graphs
4. Graph Traversal
– Depth-first traversal
– Breadth-first traversal
5. Topological Sort
6. Applications of DFS
– Bi-connectivity
– Euler circuits
– Finding Strongly Connected Components
7. Applications of BFS
– Bipartite graph
– Graph Coloring
4/6/2022 5
3.1 _ Graph Definition, Type and
Representation
6. Introduction
• A graph is a pictorial representation of a set of
objects where some pairs of objects are
connected by links.
• The interconnected objects are represented by
points termed as vertices, and the links that
connect the vertices are called edges.
• Formally, a graph is a pair of sets (V, E),
– where V is the set of vertices and
– E is the set of edges, connecting the pairs of vertices.
• Take a look at the following graph
4/6/2022
3.1 _ Graph Definition, Type and
Representation
6
9. Undirected Graph
• In an undirected graph, edges are not associated with the
directions with them.
• An undirected graph is shown in the below figure since its
edges are not attached with any of the directions.
• If an edge exists between vertex A and B then the vertices
can be traversed from B to A as well as A to B.
4/6/2022
3.1 _ Graph Definition, Type and
Representation
9
10. Directed Graph
• In a directed graph, edges form an ordered pair.
• Edges represent a specific path from some vertex A to
another vertex B.
• Node A is called initial node while node B is called
terminal node.
4/6/2022
3.1 _ Graph Definition, Type and
Representation
10
11. Directed Acyclic Graph (DAG)
• It is a directed graph , that contains no cycles.
4/6/2022
3.1 _ Graph Definition, Type and
Representation
11
12. Connected Graph
• A connected graph is the one in which some path
exists between every two vertices (u, v) in V.
• There are no isolated nodes in connected graph.
• If any isolated nodes present mean then it is
called disconnected graph.
4/6/2022
3.1 _ Graph Definition, Type and
Representation
12
13. Complete Graph
• A complete graph is the one in which every node is
connected with all other nodes.
• A complete graph contain n(n-1)/2 edges where n is the
number of nodes in the graph.
• Every vertex degree is n-1.
4/6/2022
3.1 _ Graph Definition, Type and
Representation
13
14. Weighted Graph
• In a weighted graph, each edge is assigned with
some data such as length or weight.
• The weight of an edge e can be given as w(e)
which must be a positive (+) value indicating the
cost of traversing the edge.
4/6/2022
3.1 _ Graph Definition, Type and
Representation
14
15. Digraph
• A digraph is a directed graph in which each
edge of the graph is associated with some
direction and the traversing can be done only
in the specified direction.
4/6/2022
3.1 _ Graph Definition, Type and
Representation
15
16. Graph Terminologies
• Mathematical graphs can be represented in data
structure.
• We can represent a graph using an
– array of vertices and
– a two-dimensional array of edges.
• Before we proceed further, let's familiarize
ourselves with some important terms −
– Vertex
– Edge
– Adjacency
– Path
4/6/2022
3.1 _ Graph Definition, Type and
Representation
16
17. Vertex
• Each node of the graph is
represented as a vertex.
• In the following example, the
labelled circle represents vertices.
• Thus, A to G are vertices.
• We can represent them using an
array as shown in the following
image.
• Here A can be identified by index 0.
• B can be identified using index 1 and
so on.
4/6/2022
3.1 _ Graph Definition, Type and
Representation
17
18. Edges
• Edge represents a path between two
vertices or a line between two
vertices.
• In the following example, the lines from
A to B, B to C, and so on represents
edges.
• We can use a two-dimensional array to
represent an array as shown in the
following image.
• Here AB can be represented as 1 at row
0, column 1,
• BC as 1 at row 1, column 2 and so on,
keeping other combinations as 0.
4/6/2022
3.1 _ Graph Definition, Type and
Representation
18
19. Adjacency
• Two node or vertices are
adjacent if they are connected
to each other through an
edge.
• In the following example,
– B is adjacent to A,
– C is adjacent to B, and so on.
4/6/2022
3.1 _ Graph Definition, Type and
Representation
19
20. Path
• Path represents a sequence of
edges between the two
vertices.
• In the following example,
ABCD represents a path from A
to D.
4/6/2022
3.1 _ Graph Definition, Type and
Representation
20
21. Degree of the Node
• A degree of a node is the number of edges that are
connected with that node.
• A node with degree 0 is called as isolated node.
• In the multi-graph shown on the below, the maximum
degree is 5 and the minimum degree is 0.
4/6/2022
3.1 _ Graph Definition, Type and
Representation
21
22. Loop
• An edge that is associated with the similar end
points can be called as Loop.
• In graph theory, a loop (also called a self-loop or
a buckle) is an edge that connects a vertex to
itself. A simple graph contains no loops.
4/6/2022
3.1 _ Graph Definition, Type and
Representation
22
23. Acyclic Graph, DAG, Tree
• A graph without cycles is called an acyclic graph.
• A directed graph without directed cycles is called
a directed acyclic graph.
• A connected graph without cycles is called a tree.
4/6/2022
3.1 _ Graph Definition, Type and
Representation
23
24. Maximum No. of Edges for Graphs
Graph Type Maximum No. of Edges
( m = edges , n = nodes)
Directed Graph m = n × (n – 1)
Undirected Graph m = n × (n – 1) / 2
Connected Graph m = n – 1
Tree m = n – 1
Forest m = n – c (c means no.of components)
Complete Graph m = n × (n – 1) / 2
Undirected Acyclic Graph
(Spanning Tree)
m = n – 1
Directed Acyclic Graph m = n × (n – 1) / 2
Cyclic Graph m = n
4/6/2022
3.1 _ Graph Definition, Type and
Representation
24
25. Maximum no. of Edges in Undirected Acyclic Graph
• For Undirected Acyclic Graph – It will be a spanning
tree where all the nodes are connected with no cycles
and adding one more edge will form a cycle.
• In the spanning tree, there are V-1 edges.
4/6/2022
3.1 _ Graph Definition, Type and
Representation
25
26. Maximum no. of Edges in Directed Acyclic Graph
• For Directed Acyclic Graph – Construct the graph similar to
topological order where all the edges go to one direction
and there will not be any circular dependency, means
there is no cycle.
– Take the first vertex and have a directed edge to all the other
vertices, so V-1 edges,
– second vertex to have a directed edge to rest of the vertices so
V-2 edges,
– third vertex to have a directed edge to rest of the vertices so V-
3 edges, and so on.
• This will construct a graph where all the edges in one
direction and adding one more edge will produce a cycle.
• So the total number of edges
– = (V-1) + (V-2) + (V-3) +———+2+1 = V(V-1)/2.
4/6/2022
3.1 _ Graph Definition, Type and
Representation
26
27. Maximum no. of Edges in Directed Acyclic Graph (DAG)
4/6/2022
3.1 _ Graph Definition, Type and
Representation
27
28. Graph Representation
• By Graph representation, we simply mean the
technique which is to be used in order to store
some graph into the computer's memory.
• There are two ways to store Graph into the
computer's memory.
– Sequential Representation
• Adjacency Matrix
• Incidence Matrix
– Linked Representation
• Adjacency List
4/6/2022
3.1 _ Graph Definition, Type and
Representation
28
29. Sequential Representation
• In sequential representation, we use adjacency matrix
to store the mapping represented by vertices and
edges.
• In adjacency matrix, the rows and columns are
represented by the graph vertices.
• A graph having n vertices, will have a dimension n x n.
• An entry Mij in the adjacency matrix representation of
an undirected graph G will be 1 if there exists an edge
between Vi and Vj.
4/6/2022
3.1 _ Graph Definition, Type and
Representation
29
30. Undirected Graph Representation - Adjacency Matrix
• An undirected graph and its adjacency matrix
representation is shown in the following figure.
• in the below figure, we can see the mapping among
the vertices (A, B, C, D, E) is represented by using the
adjacency matrix which is also shown in the figure.
4/6/2022
3.1 _ Graph Definition, Type and
Representation
30
31. Directed Graph Representation - Adjacency Matrix
• There exists different adjacency matrices for the directed
and undirected graph.
• In directed graph, an entry Aij will be 1 only when there is
an edge directed from Vi to Vj.
• A directed graph and its adjacency matrix representation is
shown in the following figure.
4/6/2022
3.1 _ Graph Definition, Type and
Representation
31
32. Weighted Directed Graph Representation – Adjacency Matrix
4/6/2022
3.1 _ Graph Definition, Type and
Representation
32
• Representation of weighted directed graph is different.
• Instead of filling the entry by 1, the Non- zero entries
of the adjacency matrix are represented by the weight
of respective edges.
• The weighted directed graph along with the adjacency
matrix representation is shown in the following figure.
33. Incidence Matrix
• In this representation, the graph is represented using a matrix of
size total number of vertices by a total number of edges.
• For Example, a graph with 4 vertices and 6 edges is represented
using a matrix of size 4X6.
• In this matrix, rows represent vertices and columns represents
edges.
• This matrix is filled with 0 or 1 or -1.
– 0 represents that the row edge is not connected to column vertex,
– 1 represents that the row edge is connected as the outgoing edge to
column vertex and
– -1 represents that the row edge is connected as the incoming edge to
column vertex.
• For example, consider the following directed graph representation..
4/6/2022
3.1 _ Graph Definition, Type and
Representation
33
35. Linked Representation
4/6/2022
3.1 _ Graph Definition, Type and
Representation
35
• In the linked representation, an adjacency list is
used to store the Graph into the computer's
memory.
• An adjacency list is maintained for each node
present in the graph which stores the node value
and a pointer to the next adjacent node to the
respective node.
• If all the adjacent nodes are traversed then store
the NULL in the pointer field of last node of the
list.
36. Undirected Graph Representation - Adjacency List
• The sum of the lengths of adjacency lists is equal
to the twice of the number of edges present in
an undirected graph.
• Consider the undirected graph shown in the
following figure and check the adjacency list
representation.
4/6/2022
3.1 _ Graph Definition, Type and
Representation
36
37. Directed Graph Representation - Adjacency List
4/6/2022
3.1 _ Graph Definition, Type and
Representation
37
• Consider the directed graph shown in the following
figure and check the adjacency list representation of
the graph.
• In a directed graph, the sum of lengths of all the
adjacency lists is equal to the number of edges
present in the graph.
38. Weighted Directed Graph Representation - Adjacency List
4/6/2022
3.1 _ Graph Definition, Type and
Representation
38
• In the case of weighted directed graph, each node
contains an extra field that is called the weight of the
node.
• The adjacency list representation of a directed graph is
shown in the following figure
39. Operations - Graph
• Following are basic primary operations of a
Graph −
– Add Vertex − Adds a vertex to the graph.
– Add Edge − Adds an edge between the two
vertices of the graph.
– Display Vertex − Displays a vertex of the graph.
4/6/2022
3.1 _ Graph Definition, Type and
Representation
39
40. Applications of Graph
• Graph has its applications in diverse fields of engineering −
• Electrical Engineering − The concepts of graph theory is used extensively
in designing circuit connections. The types or organization of connections
are named as topologies. Some examples for topologies are star, bridge,
series, and parallel topologies.
• Computer Science − Graph theory is used for the study of algorithms. For
example,
– Kruskal's Algorithm
– Prim's Algorithm
– Dijkstra's Algorithm
• Computer Network − The relationships among interconnected computers
in the network follows the principles of graph theory.
• Science − The molecular structure and chemical structure of a substance,
the DNA structure of an organism, etc., are represented by graphs.
• Linguistics − The parsing tree of a language and grammar of a language
uses graphs.
• General − Routes between the cities can be represented using graphs.
Depicting hierarchical ordered information such as family tree can be used
as a special type of graph called tree.
4/6/2022
3.1 _ Graph Definition, Type and
Representation
40
42. UNIT III : GRAPHS
By
Mr.S.Selvaraj
Asst. Professor (SRG) / CSE
Kongu Engineering College
Perundurai, Erode, Tamilnadu, India
Thanks to and Resource from : Data Structures and Algorithm Analysis in C by Mark Allen Weiss & Sumitabha Das, “Computer Fundamentals and C
Programming”, 1st Edition, McGraw Hill, 2018.
20CST32 – Data Structures
43. Unit III : Contents
1. Graph Definitions
2. Types of Graph
3. Representation of Graphs
4. Graph Traversal
– Depth-first traversal
– Breadth-first traversal
5. Topological Sort
6. Applications of DFS
– Bi-connectivity
– Euler circuits
– Finding Strongly Connected Components
7. Applications of BFS
– Bipartite graph
– Graph Coloring
4/6/2022 43
3.2 _ Graph Traversals
46. Depth First Search (DFS)
• Depth First Search (DFS) algorithm traverses a
graph in a depthward motion.
• It uses a stack to remember to get the next
vertex to start a search, when a dead end
occurs in any iteration.
4/6/2022 3.2 _ Graph Traversals 46
47. DFS - Rules
• As in the example given below, DFS
algorithm traverses from S to A to D
to G to E to B first, then to F and lastly
to C. It employs the following rules.
• Rule 1 − Visit the adjacent unvisited
vertex. Mark it as visited. Display it.
Push it in a stack.
• Rule 2 − If no adjacent vertex is found,
pop up a vertex from the stack. (It will
pop up all the vertices from the stack,
which do not have adjacent vertices.)
• Rule 3 − Repeat Rule 1 and Rule 2
until the stack is empty.
4/6/2022 3.2 _ Graph Traversals 47
49. • Step 2:
– Mark S as visited and put it onto the stack.
– Explore any unvisited adjacent node from S.
– We have three nodes and we can pick any of them.
– For this example, we shall take the node in an
alphabetical order.
4/6/2022 3.2 _ Graph Traversals 49
50. • Step 3:
– Mark A as visited and put it onto the stack.
– Explore any unvisited adjacent node from A.
– Both S and D are adjacent to A but we are concerned
for unvisited nodes only.
4/6/2022 3.2 _ Graph Traversals 50
51. • Step 4:
– Visit D and mark it as visited and put onto the stack.
– Here, we have B and C nodes, which are adjacent to D
and both are unvisited.
– However, we shall again choose in an alphabetical
order.
4/6/2022 3.2 _ Graph Traversals 51
52. • Step 5:
– We choose B, mark it as visited and put onto the
stack.
– Here B does not have any unvisited adjacent node.
– So, we pop B from the stack.
4/6/2022 3.2 _ Graph Traversals 52
53. • Step 6:
– We check the stack top for return to the previous
node and check if it has any unvisited nodes.
– Here, we find D to be on the top of the stack.
4/6/2022 3.2 _ Graph Traversals 53
54. • Step 7:
– Only unvisited adjacent node is from D is C now.
– So we visit C, mark it as visited and put it onto the
stack.
4/6/2022 3.2 _ Graph Traversals 54
55. DFS – C Program
• #include<stdio.h>
• #include<conio.h>
• int a[20][20],reach[20],n;
• void dfs(int v) {
• int i;
• reach[v]=1;
• for (i=1;i<=n;i++)
• if(a[v][i] && !reach[i]) {
• printf("n %d->%d",v,i);
• dfs(i);
• }
• }
4/6/2022 3.2 _ Graph Traversals 55
56. • void main() {
• int i,j,count=0;
• printf("n Enter number of vertices:");
• scanf("%d",&n);
• for (i=1;i<=n;i++) {
• reach[i]=0;
• for (j=1;j<=n;j++)
• a[i][j]=0;
• }
• printf("n Enter the adjacency matrix:n");
• for (i=1;i<=n;i++)
• for (j=1;j<=n;j++)
• scanf("%d",&a[i][j]);
4/6/2022 3.2 _ Graph Traversals 56
57. • dfs(1);
• printf("n");
• for (i=1;i<=n;i++) {
• if(reach[i])
• count++;
• }
•
• if(count==n)
• printf("n Graph is connected");
• else
• printf("n Graph is not connected");
• }
4/6/2022 3.2 _ Graph Traversals 57
58. Breadth First Search (BFS)
• Breadth First Search (BFS) algorithm traverses
a graph in a breadthward motion.
• It uses a queue to remember to get the next
vertex to start a search, when a dead end
occurs in any iteration.
4/6/2022 3.2 _ Graph Traversals 58
59. BFS - Rules
• As in the example given above, BFS
algorithm traverses from A to B to E
to F first then to C and G lastly to D.
It employs the following rules.
• Rule 1 − Visit the adjacent unvisited
vertex. Mark it as visited. Display it.
Insert it in a queue.
• Rule 2 − If no adjacent vertex is
found, remove the first vertex from
the queue.
• Rule 3 − Repeat Rule 1 and Rule 2
until the queue is empty.
4/6/2022 3.2 _ Graph Traversals 59
60. BFS - Example
• Step 1: Initialize the queue.
4/6/2022 3.2 _ Graph Traversals 60
61. • Step 2:
– We start from visiting S (starting node), and mark
it as visited.
4/6/2022 3.2 _ Graph Traversals 61
62. • Step 3:
– We then see an unvisited adjacent node from S.
– In this example, we have three nodes but
alphabetically we choose A, mark it as visited and
enqueue it.
4/6/2022 3.2 _ Graph Traversals 62
63. • Step 4:
– Next, the unvisited adjacent node from S is B.
– We mark it as visited and enqueue it.
4/6/2022 3.2 _ Graph Traversals 63
64. • Step 5:
– Next, the unvisited adjacent node from S is C.
– We mark it as visited and enqueue it.
4/6/2022 3.2 _ Graph Traversals 64
65. • Step 6:
– Now, S is left with no unvisited adjacent nodes.
– So, we dequeue and find A.
4/6/2022 3.2 _ Graph Traversals 65
66. • Step 7:
– From A we have D as unvisited adjacent node.
– We mark it as visited and enqueue it.
4/6/2022 3.2 _ Graph Traversals 66
67. BFS – C Program
• #include<stdio.h>
• int a[20][20], q[20], visited[20], n, i, j, f = 0, r = -1;
• void bfs(int v) {
• for(i = 1; i <= n; i++)
• if(a[v][i] && !visited[i])
• q[++r] = i;
• if(f <= r) {
• visited[q[f]] = 1;
• bfs(q[f++]);
• }
• }
4/6/2022 3.2 _ Graph Traversals 67
68. • void main() {
• int v;
• printf("n Enter the number of vertices:");
• scanf("%d", &n);
•
• for(i=1; i <= n; i++) {
• q[i] = 0;
• visited[i] = 0;
• }
•
• printf("n Enter graph data in matrix form:n");
• for(i=1; i<=n; i++) {
• for(j=1;j<=n;j++) {
• scanf("%d", &a[i][j]);
• }
• }
•
4/6/2022 3.2 _ Graph Traversals 68
69. • printf("n Enter the starting vertex:");
• scanf("%d", &v);
• bfs(v);
• printf("n The node which are reachable are:n");
•
• for(i=1; i <= n; i++) {
• if(visited[i])
• printf("%dt", i);
• else {
• printf("n Bfs is not possible. Not all nodes are reachable");
• break;
• }
• }
• }
4/6/2022 3.2 _ Graph Traversals 69
71. UNIT III : GRAPHS
By
Mr.S.Selvaraj
Asst. Professor (SRG) / CSE
Kongu Engineering College
Perundurai, Erode, Tamilnadu, India
Thanks to and Resource from : Data Structures and Algorithm Analysis in C by Mark Allen Weiss & Sumitabha Das, “Computer Fundamentals and C
Programming”, 1st Edition, McGraw Hill, 2018.
20CST32 – Data Structures
72. Unit III : Contents
1. Graph Definitions
2. Types of Graph
3. Representation of Graphs
4. Graph Traversal
– Depth-first traversal
– Breadth-first traversal
5. Topological Sort
6. Applications of DFS
– Bi-connectivity
– Euler circuits
– Finding Strongly Connected Components
7. Applications of BFS
– Bipartite graph
– Graph Coloring
4/6/2022 72
3.3 _ Topological Sort
73. Topological Sort
• A topological sort is an ordering of vertices in a
directed acyclic graph, such that if there is a path
from vi to vj, then vj appears after vi in the
ordering.
• The graph in this slide represents the course
prerequisite structure at a state university in
Miami.
• A directed edge (v,w) indicates that course v must
be completed before course w may be
attempted.
• A topological ordering of these courses is any
course sequence that does not violate the
prerequisite requirement.
• It is clear that a topological ordering is not
possible if the graph has a cycle, since for two
vertices v and w on the cycle, v precedes w and w
precedes v.
• Furthermore, the ordering is not necessarily
unique; any legal ordering will do.
4/6/2022 3.3 _ Topological Sort 73
74. Example
• In the given graph v1, v2, v5, v4, v3, v7, v6 and v1, v2,
v5, v4, v7, v3, v6 are both topological orderings.
• A simple algorithm to find a topological ordering is first
to find any vertex with no incoming edges.
• We can then print this vertex, and remove it, along
with its edges, from the graph. Then we apply this
same strategy to the rest of the graph.
4/6/2022 3.3 _ Topological Sort 74
75. Pseudo code for Topological Sort
• To formalize this, we define the indegree
of a vertex v as the number of edges (u,v).
• We compute the indegrees of all vertices
in the graph.
• Assuming that the indegree array is
initialized and that the graph is read into
an adjacency list, we can then apply the
algorithm to generate a topological
ordering.
• The function
find_new_vertex_of_indegree_zero scans
the indegree array looking for a vertex
with indegree 0 that has not already been
assigned a topological number.
• It returns NOT_A_VERTEX if no such vertex
exists; this indicates that the graph has a
cycle.
4/6/2022 3.3 _ Topological Sort 75
76. Adjacency Matrix - Time Complexity – Topological sort
• Because find_new_vertex_of_indegree_zero is a simple sequential
scan of the indegree array, each call to it takes O(|V|) time. Since
there are |V| such calls, the running time of the algorithm is
O(|V|2).
• If the graph is sparse, we would expect that only a few vertices have
their indegrees updated during each iteration.
• However, in the search for a vertex of indegree 0, we look at
(potentially) all the vertices, even though only a few have changed.
• We can remove this inefficiency by keeping all the (unassigned)
vertices of indegree 0 in a special box.
• The find_new_vertex_of_indegree_zero function then returns (and
removes) any vertex in the box.
• When we decrement the indegrees of the adjacent vertices, we
check each vertex and place it in the box if its indegree falls to 0.
4/6/2022 3.3 _ Topological Sort 76
78. Adjacency List - Time Complexity – Topological Sort
• To implement the box, we can use either a stack
or a queue.
• First, the indegree is computed for every vertex.
• Then all vertices of indegree 0 are placed on an
initially empty queue.
• While the queue is not empty, a vertex v is
removed, and all edges adjacent to v have their
indegrees decremented.
• A vertex is put on the queue as soon as its
indegree falls to 0.
• The topological ordering then is the order in
which the vertices dequeue. Side image shows the
status after each phase.
• We will assume that the graph is already read into
an adjacency list and that the indegrees are
computed and placed in an array.
• The time to perform this algorithm is O(|E| + |V|)
if adjacency lists are used.
4/6/2022 3.3 _ Topological Sort 78
86. UNIT III : GRAPHS
By
Mr.S.Selvaraj
Asst. Professor (SRG) / CSE
Kongu Engineering College
Perundurai, Erode, Tamilnadu, India
Thanks to and Resource from : Data Structures and Algorithm Analysis in C by Mark Allen Weiss & Sumitabha Das, “Computer Fundamentals and C
Programming”, 1st Edition, McGraw Hill, 2018.
20CST32 – Data Structures
87. Unit III : Contents
1. Graph Definitions
2. Types of Graph
3. Representation of Graphs
4. Graph Traversal
– Depth-first traversal
– Breadth-first traversal
5. Topological Sort
6. Applications of DFS
– Bi-connectivity
– Euler circuits
– Finding Strongly Connected Components
7. Applications of BFS
– Bipartite graph
– Graph Coloring
4/6/2022 87
3.4 _ Applications of DFS
88. Applications of DFS
1. To produce Minimum Spanning Tree and all pair shortest path
tree.
2. Detecting cycle in a graph:
– A graph has cycle if and only if we see a back edge during DFS.
– So we can run DFS for the graph and check for back edges.
3. Path Finding :
– We can specialize the DFS algorithm to find a path between two
given vertices u and z.
i) Call DFS(G, u) with u as the start vertex.
ii) Use a stack S to keep track of the path between the start vertex and the current
vertex.
iii) As soon as destination vertex z is encountered, return the path as the contents
of the stack
4. Topological Sorting:
– Topological Sorting is mainly used for scheduling jobs from the given
dependencies among jobs.
4/6/2022 3.4 _ Applications of DFS 88
89. Applications of DFS
4. To test if a graph is bipartite:
– We can augment either BFS or DFS when we first discover a
new vertex, color it opposited its parents, and for each other
edge, check it doesn’t link two vertices of the same color.
– The first vertex in any connected component can be red or
black!
5. Finding Strongly Connected Components of a graph:
– A directed graph is called strongly connected, if there is a path
from each vertex in the graph to every other vertex.
6. Solving puzzles with only one solution, such as mazes.
4/6/2022 3.4 _ Applications of DFS 89
90. Minimum Spanning Tree
• Finding a minimum spanning tree in an undirected makes sense for
directed graphs but appears to be more difficult.
• Informally, a minimum spanning tree of an undirected graph G is a tree
formed from graph edges that connects all the vertices of G at lowest total
cost.
• A minimum spanning tree exists if and only if G is connected.
• Although a robust algorithm should report the case that G is unconnected.
• Notice that the number of edges in the minimum spanning tree is |V| - 1.
• The minimum spanning tree is a tree because it is acyclic.
• The minimum spanning tree is spanning because it covers every edge, and
it is minimum for the obvious reason.
• If we need to wire a house with a minimum of cable, then a minimum
spanning tree problem needs to be solved.
• There are two basic algorithms to solve this problem; both are greedy.
We now describe them.
– Prim's Algorithm
– Kruskal's Algorithm
4/6/2022 3.4 _ Applications of DFS 90
91. Greedy Approach
• An algorithm is designed to achieve optimum solution for a given
problem.
• In greedy algorithm approach, decisions are made from the given
solution domain.
• As being greedy, the closest solution that seems to provide an optimum
solution is chosen.
• Greedy algorithms try to find a localized optimum solution, which may
eventually lead to globally optimized solutions.
• However, generally greedy algorithms do not provide globally optimized
solutions.
• Examples:
– Travelling Salesman Problem
– Prim's Minimal Spanning Tree Algorithm
– Kruskal's Minimal Spanning Tree Algorithm
– Dijkstra's Minimal Spanning Tree Algorithm
– Graph - Map Coloring
– Graph - Vertex Cover
– Knapsack Problem
– Job Scheduling Problem
– Huffman Coding
4/6/2022 3.4 _ Applications of DFS 91
93. MST – Prim’s Algorithm
• The algorithm then finds, at each stage, a new
vertex to add to the tree by choosing the
edge (u, v) such that the cost of (u, v) is the
smallest among all edges where u is in the
tree and v is not.
4/6/2022 3.4 _ Applications of DFS 93
94. Example – Prim’s Algorithm
4/6/2022 3.4 _ Applications of DFS 94
95. Prim’s Algorithm Table Formation
• For each vertex we keep values dv and pv and an indication
of whether it is known or unknown.
• dv is the weight of the shortest arc connecting v to a
known vertex, and pv, as before, is the last vertex to cause a
change in dv.
• The rest of the algorithm is exactly the same, with the
exception that since the definition of dv is different, so is
the update rule.
• For this problem, the update rule is even simpler than
before: After a vertex v is selected, for each unknown w
adjacent to v
4/6/2022 3.4 _ Applications of DFS 95
97. Prim’s Algorithm Table - Explanation
a) In the initial configuration of the table.
b) Next v1 is selected, and v2, v3, and v4 are updated.
c) The next vertex selected is v4. Every vertex is adjacent to
v4 are updated. v1 is not examined, because it is known.
v2 is unchanged, because it has dv = 2 and the edge cost
from v4 to v2 is 3; all the rest are updated.
d) The next vertex chosen is v2 (arbitrarily breaking a tie).
This does not affect any distances. Then v3 is chosen,
which affects the distance in v6.
e) Next selection of v7, which forces v6 and v5 to be
adjusted.
f) Next v6 and then v5 are selected, completing the
algorithm.
4/6/2022 3.4 _ Applications of DFS 97
98. Prim’s Algorithm – Cost and Time Complexity
• In the final table , the edges in the spanning tree can be read from
the table: (v2, v1), (v3, v4), (v4, v1), (v5, v7), (v6, v7), (v7, v4).
• The total cost is 16.
• The entire implementation of this algorithm is virtually identical to
that of Dijkstra‘s algorithm, and everything that was said about
the analysis of Dijkstra's algorithm applies here.
• Be aware that Prim's algorithm runs on undirected graphs, so
when coding it, remember to put every edge in two adjacency
lists.
• The running time is
– O (|V|2) without heaps, which is optimal for dense graphs, and
(Adjacency Matrix)
– O (|E| log |V|) using binary heaps, which is good for sparse graphs.
(Adjacency List)
– O (|E| + |V| log |V|) using fibonacci heap. (Adjacency List)
4/6/2022 3.4 _ Applications of DFS 98
99. Kruskal’s Algorithm
• A second greedy strategy is continually to
select the edges in order of smallest weight
• Accept an edge if it does not cause a cycle.
4/6/2022 3.4 _ Applications of DFS 99
100. Kruskal’s Algorithm
• Formally, Kruskal's algorithm maintains a forest -- a collection of
trees.
• Initially, there are |V| single-node trees.
• Adding an edge merges two trees into one. When the algorithm
terminates, there is only one tree, and this is the minimum
spanning tree.
• If u and v are in the same set, the edge is rejected, because since
they are already connected, adding (u, v) would form a cycle.
Otherwise, the edge is accepted, and a union is performed on the
two sets containing u and v.
• On some machines it is more efficient to implement the priority
queue as an array of pointers to edges, rather than as an array of
edges.
• The effect of this implementation is that, to rearrange the heap,
only pointers, not large records, need to be moved.
4/6/2022 3.4 _ Applications of DFS 100
102. Kruskal’s Algorithm – Time Complexity
• The worst-case running time of this algorithm is
O(|E| log |E|), which is dominated by the heap
operations.
• Notice that since |E| = O(|V|2), this running time
is actually O(|E| log |V|).
• In practice, the algorithm is much faster than this
time bound would indicate.
4/6/2022 3.4 _ Applications of DFS 102
104. Biconnectivity
• A connected undirected graph is biconnected if there are no
vertices whose removal disconnects the rest of the graph.
• If the nodes are computers and the edges are links, then if any
computer goes down, network mail is unaffected, except, of
course, at the down computer.
• Similarly, if a mass transit system is biconnected, users always
have an alternate route should some terminal be disrupted.
• If a graph is not biconnected, the vertices whose removal
would disconnect the graph are known as articulation points.
These nodes are critical in many applications.
4/6/2022 3.4 _ Applications of DFS 104
106. Answer
• The graph is not biconnected.
• C and D are articulation points.
• The removal of C would disconnect G, and the
removal of D would disconnect E and F, from
the rest of the graph.
4/6/2022 3.4 _ Applications of DFS 106
109. Example 2 – DFS Spanning Tree with Back edge
4/6/2022 3.4 _ Applications of DFS 109
110. Finding Articulation Points - Steps to follow
• Depth-first search provides a linear-time algorithm to find
all articulation points in a connected graph.
• First, starting at any vertex, we perform a depth-first
search and number the nodes as they are visited.
• For each vertex v, we call this preorder number num(v).
• Then, for every vertex v in the depth-first search spanning
tree, we compute the lowest-numbered vertex, which we
call low(v), that is reachable from v by taking zero or more
tree edges and then possibly one back edge (in that order).
• The depth-first search tree in the previous side shows the
preorder number first, and then the lowest-numbered
vertex reachable under the rule.
4/6/2022 3.4 _ Applications of DFS 110
111. Finding Articulation Points - Steps to follow
• The lowest-numbered vertex reachable by A, B, and C is vertex 1 (A), because they
can all take tree edges to D and then one back edge back to A.
• We can efficiently compute low by performing a postorder traversal of the depth-
first spanning tree.
• By the definition of low, low(v) is the minimum of
– 1. num(v)
– 2. the lowest num(w) among all back edges (v, w)
– 3. the lowest low(w) among all tree edges (v, w)
• The first condition is the option of taking no edges, the second way is to choose no
tree edges and a back edge, and the third way is to choose some tree edges and
possibly a back edge.
• This third method is succinctly described with a recursive call. Since we need to
evaluate low for all the children of v before we can evaluate low(v), this is a
postorder traversal.
• For any edge (v,w), we can tell whether it is a tree edge or a back edge merely by
checking num(v) and num(w). Thus, it is easy to compute low(v): we merely scan
down v's adjacency list, apply the proper rule, and keep track of the minimum.
• Doing all the computation takes O(|E| +|V|) time.
4/6/2022 3.4 _ Applications of DFS 111
112. Finding Articulation Points - Steps to follow
• All that is left to do is to use this information to find articulation points.
• The root is an articulation point if and only if it has more than one child,
because if it has two children, removing the root disconnects nodes in
different subtrees, and if it has only one child, removing the root merely
disconnects the root.
• Any other vertex v is an articulation point if and only if v has some child
w such that low(w)>= num(v).
• Notice that this condition is always satisfied at the root; hence the need
for a special test.
• The if part of the proof is clear when we examine the articulation points
that the algorithm determines, namely C and D. D has a child E, and
low(E)>=num(D), since both are 4. Thus, there is only one way for E to get
to any node above D, and that is by going through D. So D is an
articulation point.
• Similarly, C is an articulation point, because low (G) num (C). To prove that
this algorithm is correct, one must show that the only if part of the
assertion is true (that is, this finds all articulation points).
4/6/2022 3.4 _ Applications of DFS 112
113. Biconectivity Graph – Routine to Assign num and
Compute parents
/* assign num and compute parents */
Void assign_num( vertex v )
{
vertex w;
num[v] = counter++;
visited[v] = TRUE;
for each w adjacent to v
if( !visited[w] )
{
parent[w] = v;
assign_num( w );
}
}
4/6/2022 3.4 _ Applications of DFS 113
114. /* assign low. Also check for articulation points */
Void assign_low( vertex v )
{
vertex w;
low[v] = num[v]; /* Rule 1 */
for each w adjacent to v
{
if( num[w] > num[v] ) /* forward edge */
{
assign_low( w );
if( low[w] >= num[v] )
printf( "%v is an articulation pointn", v );
low[v] = min( low[v], low[w] ); /* Rule 3 */
}
Else
if( parent[v] != w ) /* back edge */
low[v] = min( low[v], num[w] ); /* Rule 2 */
}
}
4/6/2022 3.4 _ Applications of DFS 114
Biconectivity Graph – Routine to Assign low and also
check for articulation points
115. Biconectivity Graph – Testing for for articulation points in one
DFS Spanning Tree (Root is Omitted)
Void find_art( vertex v )
{
vertex w;
visited[v] = TRUE;
low[v] = num[v] = counter++; /* Rule 1 */
for each w adjacent to v
{
if( !visited[w] ) /* forward edge */
{
parent[w] = v;
find_art( w );
if( low[w] >= num[v] )
printf ( "%v is an articulation pointn", v );
low[v] = min( low[v], low[w] ); /* Rule 3 */
}
Else
if( parent[v] != w ) /* back edge */
low[v] = min( low[v], num[w] ); /* Rule 2 */
}
}
4/6/2022 3.4 _ Applications of DFS 115
116. Euler Graph
• Consider the three figures in this slide.
• A popular puzzle is to reconstruct these figures using a pen, drawing each
line exactly once.
• The pen may not be lifted from the paper while the drawing is being
performed.
• As an extra challenge, make the pen finish at the same point at which it
started.
• This puzzle has a surprisingly simple solution. Stop reading if you would
like to try to solve it.
4/6/2022 3.4 _ Applications of DFS 116
117. Solution
• The first figure can be drawn only if the
starting point is the lower left- or right-hand
corner, and it is not possible to finish at the
starting point.
• The second figure is easily drawn with the
finishing point the same as the starting point.
• Third figure cannot be drawn at all within the
parameters of the puzzle.
4/6/2022 3.4 _ Applications of DFS 117
118. If you want extra... Try this.... And Find out
4/6/2022 3.4 _ Applications of DFS 118
119. About Euler Circuit Problem
• We can convert this problem to a graph theory problem by assigning a
vertex to each intersection.
• Then the edges can be assigned in the natural manner, as in After this
conversion is performed, we must find a path in the graph that visits every
edge exactly once. If we are to solve the "extra challenge," then we must
find a cycle that visits every edge exactly once.
• This graph problem was solved in 1736 by Euler and marked the beginning
of graph theory.
• The problem is thus commonly referred to as an Euler path (sometimes
Euler tour) or Euler circuit problem, depending on the specific problem
statement.
• The Euler tour and Euler circuit problems, though slightly different, have
the same basic solution. Thus, we will consider the Euler circuit problem
in this section.
4/6/2022 3.4 _ Applications of DFS 119
120. Euler Circuit - Rules
• The first observation that can be made is that an Euler circuit, which
must end on its starting vertex, is possible only if the graph is
connected and each vertex has an even degree (number of edges).
• This is because, on the Euler circuit, a vertex is entered and then
left. If any vertex v has odd degree, then eventually we will reach
the point where only one edge into v is unvisited, and taking it will
strand us at v.
• If exactly two vertices have odd degree, an Euler tour, which must
visit every edge but need not return to its starting vertex, is still
possible if we start at one of the odd-degree vertices and finish at
the other. (Also Called Semi-Euler)
• If more than two vertices have odd degree, then an Euler tour is
not possible.
4/6/2022 3.4 _ Applications of DFS 120
121. • We can assume that we know that an Euler circuit exists, since we
can test the necessary and sufficient condition in linear time. Then
the basic algorithm is to perform a depth-first search.
• There is a surprisingly large number of "obvious" solutions that do
not work. Some of these are presented in the exercises.
• The main problem is that we might visit a portion of the graph and
return to the starting point prematurely.
• If all the edges coming out of the start vertex have been used up,
then part of the graph is untraversed.
• The easiest way to fix this is to find the first vertex on this path that
has an untraversed edge, and perform another depth-first search.
• This will give another circuit, which can be spliced into the original.
• This is continued until all edges have been traversed.
4/6/2022 3.4 _ Applications of DFS 121
122. Example – Euler Circuit
4/6/2022 3.4 _ Applications of DFS 122
126. Euler Circuit - Time Complexity
• To make splicing simple, the path should be maintained as a linked
list.
• To avoid repetitious scanning of adjacency lists, we must maintain,
for each adjacency list, a pointer to the last edge scanned.
• When a path is spliced in, the search for a new vertex from which to
perform the next dfs must begin at the start of the splice point.
• This guarantees that the total work performed on the vertex search
phase is O(|E|) during the entire life of the algorithm.
• With the appropriate data structures, the running time of the
algorithm is O(|E| + |V|).
• A very similar problem is to find a simple cycle, in an undirected
graph, that visits every vertex exactly once. This is known as the
Hamiltonian cycle problem. Although it seems almost identical to
the Euler circuit problem, no efficient algorithm for it is known.
4/6/2022 3.4 _ Applications of DFS 126
127. Hamiltonian Graph
• Hamiltonian Circuit – A simple circuit in Graph G that passes
through every vertex by exactly once.
• Starting vertex can be visited twice.
• An edge can be visited twice.
• There is no fixed solution to find out the Hamiltonian circuit exists
in a graph or not.
• It is NP Complete. There is no polynomial time solution.
• A complete graph have more than two vertex’s is an Hamiltonian
graph. It always have Hamiltonian circuit.
• A cycle graph is also have Hamiltonian Circuit.
• Heuristic approach is used to find Hamiltonian Circuit. ( i.e Trial and
Error approach).
• Hamiltonian Path – A path visits every vertex once but cant return
back to starting vertex is called Hamiltonian Path.
• Dirac’s Theorem and Ore’s Theorem are used to give clue weather
Hamiltonian circuit is present in a graph or not.
4/6/2022 3.4 _ Applications of DFS 127
129. Strongly Connected Graph
• A directed graph is called strongly connected
if there is a path in each direction between
each pair of vertices of the graph.
• That is, a path exists from the first vertex in
the pair to the second, and another path
exists from the second vertex to the first.
4/6/2022 3.4 _ Applications of DFS 129
130. Connected and Strongly Connected Component
• Component of a graph in which every vertex has a path to other
vertex in that component.
• If we can reach from every vertex to every other vertex in a
component then its called Strongly Connected Component (SCC).
4/6/2022 3.4 _ Applications of DFS 130
131. SCG in Undirected Component
• All the nodes in a component are always
strongly connected in an undirected graph.
4/6/2022 3.4 _ Applications of DFS 131
132. SCG in Directed Component
Strongly Connected Component or Not... Find out.....
4/6/2022 3.4 _ Applications of DFS 132
133. Another Example
• Is it Strongly Connected Graph (SCG)?
• If No, Then how many Strongly Connected Components
(SCCs) exists and what are those?
4/6/2022 3.4 _ Applications of DFS 133
135. Finding Strongly Connected Components (SCC) Using DFS
• If the graph is not strongly connected, a depth-first
search starting at some node might not visit all nodes.
• In this case we repeatedly perform depth-first
searches, starting at some unmarked node, until all
vertices have been visited.
• Single node is always strongly connected component.
4/6/2022 3.4 _ Applications of DFS 135
136. Example - Strongly Connected Component
4/6/2022 3.4 _ Applications of DFS 136
137. Step 1: DFS Spanning Forest
• We arbitrarily start the depth-first search at vertex B.
• This visits vertices B, C, A, D, E, and F.
• We then restart at some unvisited vertex. Arbitrarily, we
start at H, which visits I and J.
• Finally, we start at G, which is the last vertex that needs to
be visited.
• The corresponding depth-first search tree is shown below.
4/6/2022 3.4 _ Applications of DFS 137
138. DFS Spanning Forest Formation using the three
types of edges
• The dashed arrows in the depth-first spanning forest are edges (v, w) for
which w was already marked at the time of consideration.
• In undirected graphs, these are always back edges, but, as we can see,
there are three types of edges that do not lead to new vertices.
• First, there are back edges, such as (A, B) and (I, H).
• There are also forward edges, such as (C, D), (C, E) and (F,C) that lead from
a tree node to a descendant.
• Finally, there are cross edges, such as (G, F), (G, H), and (H,F) which
connect two tree nodes that are not directly related.
• Depth-first search forests are generally drawn with children and new trees
added to the forest from left to right.
• In a depth- first search of a directed graph drawn in this manner, cross
edges always go from right to left.
4/6/2022 3.4 _ Applications of DFS 138
139. Step 2: Postorder Traverse of DFST
4/6/2022 3.4 _ Applications of DFS 139
• By performing two depth-first searches, we can test
whether a directed graph is strongly connected, and if
it is not, we can actually produce the subsets of
vertices that are strongly connected to themselves.
• This can also be done in only one depth-first search,
but the method used here is much simpler to
understand.
• First, a depth-first search is performed on the input
graph G.
• The vertices of G are numbered by a postorder
traversal of the depth-first spanning forest, and then
all edges in G are reversed, forming Gr.
140. Step 3: Reversed Graph (Gr) by Change
the direction of edges into reverse
4/6/2022 3.4 _ Applications of DFS 140
141. Step 4: Perform DFS from Highest-
numbered vertex.
• The algorithm is completed by performing a depth-first
search on Gr, always starting a new depth first search
at the highest-numbered vertex.
• Thus, we begin the depth-first search of Gr at vertex G,
which is numbered 10.
• This leads nowhere, so the next search is started at H.
This call visits I and J.
• The next call starts at B and visits A, C, and F.
• The next calls after this are dfs(D) and finally dfs(E).
4/6/2022 3.4 _ Applications of DFS 141
143. Step 6: List Strongly Connected Components
• Each of the trees (this is easier to see if you
completely ignore all nontree edges) in this depth-
first spanning forest forms a strongly connected
component.
• Thus, for our example, the strongly connected
components are {G}, {H, I, J}, {B, A, C, F}, {D}, and
{E}.
• We can find all strongly connected components in
O(V+E) time using Kosaraju’s algorithm.
4/6/2022 3.4 _ Applications of DFS 143
145. UNIT III : GRAPHS
By
Mr.S.Selvaraj
Asst. Professor (SRG) / CSE
Kongu Engineering College
Perundurai, Erode, Tamilnadu, India
Thanks to and Resource from : Data Structures and Algorithm Analysis in C by Mark Allen Weiss & Sumitabha Das, “Computer Fundamentals and C
Programming”, 1st Edition, McGraw Hill, 2018.
20CST32 – Data Structures
146. Unit III : Contents
1. Graph Definitions
2. Types of Graph
3. Representation of Graphs
4. Graph Traversal
– Depth-first traversal
– Breadth-first traversal
5. Topological Sort
6. Applications of DFS
– Bi-connectivity
– Euler circuits
– Finding Strongly Connected Components
7. Applications of BFS
– Bipartite graph
– Graph Coloring
4/6/2022 146
3.5 _ Applications of BFS
147. Applications of BFS
• Shortest Path and Minimum Spanning Tree for unweighted grap. In an
unweighted graph, the shortest path is the path with least number of edges. With
Breadth First, we always reach a vertex from given source using the minimum
number of edges. Also, in case of unweighted graphs, any spanning tree is
Minimum Spanning Tree and we can use either Depth or Breadth first traversal for
finding a spanning tree
• Peer to Peer Networks. In Peer to Peer Networks like BitTorrent, Breadth First
Search is used to find all neighbor nodes.
• Crawlers in Search Engines: Crawlers build index using Breadth First.
• Social Networking Websites: In social networks, we can find people within a given
distance ‘k’ from a person using Breadth First Search till ‘k’ levels.
• GPS Navigation systems: Breadth First Search is used to find all neighboring
locations.
• Broadcasting in Network: In networks, a broadcasted packet follows Breadth First
Search to reach all nodes.
• In Garbage Collection: Breadth First Search is used in copying garbage collection
using Cheney’s algorithm.
4/6/2022 3.5 _ Applications of BFS 147
148. Applications of BFS
• Cycle detection in undirected graph: In undirected graphs, either
Breadth First Search or Depth First Search can be used to detect
cycle. We can use BFS to detect cycle in a directed graph also,
• Ford–Fulkerson algorithm In Ford-Fulkerson algorithm, we can
either use Breadth First or Depth First Traversal to find the
maximum flow.
• To test if a graph is Bipartite We can either use Breadth First or
Depth First Traversal.
• Path Finding We can either use Breadth First or Depth First
Traversal to find if there is a path between two vertices.
• Finding all nodes within one connected component: We can either
use Breadth First or Depth First Traversal to find all nodes reachable
from a given node.
4/6/2022 3.5 _ Applications of BFS 148
149. Bipartite Graph
• A Bipartite Graph is one whose vertices can be divided into
disjoint and independent sets, say U and V, such that every
edge has one vertex in U and the other in V.
• In a bipartite graph, we have two sets of
vertices U and V (known as bipartitions) and each edge is
incident on one vertex in U and one vertex in V.
• There will not be any edges connecting two vertices
in U or two vertices in V.
• The algorithm to determine whether a graph is bipartite or
not uses the concept of graph colouring and BFS.
• O(V+E) time complexity on using an adjacency list and
O(V^2) on using adjacency matrix.
4/6/2022 3.5 _ Applications of BFS 149
150. Bipartite Graph - Examples
4/6/2022 3.5 _ Applications of BFS 150
• Here is an example of a bipartite graph (left), and an example of a graph that is not
bipartite(Right).
• Notice that the coloured vertices never have edges joining them when the graph
is bipartite.
151. Bipartite Graph - Algorithm
• A bipartite graph is possible if it is possible to assign a colour to
each vertex such that no two neighbour vertices are assigned the
same colour.
• Only two colours can be used in this process.
• Steps:
1. Assign a colour(say red) to the source vertex.
2. Assign all the neighbours of the above vertex another colour(say
blue).
3. Taking one neighbour at a time, assign all the neighbour's
neighbours the colour red.
4. Continue in this manner till all the vertices have been assigned a
colour.
5. If at any stage, we find a neighbour which has been assigned the
same colour as that of the current vertex, stop the process. The
graph cannot be coloured using two colours. Thus the graph is not
bipartite.
4/6/2022 3.5 _ Applications of BFS 151
153. Graph Coloring
• Graph coloring problem is to assign colors to certain
elements of a graph subject to certain constraints.
• Vertex coloring is the most common graph coloring
problem. The problem is, given m colors, find a way of
coloring the vertices of a graph such that no two
adjacent vertices are colored using same color.
• The other graph coloring problems like Edge
Coloring (No vertex is incident to two edges of same
color) and
• Face Coloring (Geographical Map Coloring) can be
transformed into vertex coloring.
4/6/2022 3.5 _ Applications of BFS 153
154. Chromatic Number:
• Chromatic Number: The smallest number of
colors needed to color a graph G is called its
chromatic number.
• For example, the following can be colored
minimum 2 colors.
4/6/2022 3.5 _ Applications of BFS 154
155. How many Colors are needed to color
this graph?
4/6/2022 3.5 _ Applications of BFS 155