0% found this document useful (0 votes)
7 views

Unit-4 (1)

Uploaded by

nmxbusiness0
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Unit-4 (1)

Uploaded by

nmxbusiness0
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 75

Program: B.

Tech
Course Code: AIML303/AIDS303/IOT303
Course Name: Design & Analysis of Algorithms

Unit-4

By:
Dr. Monika Bansal
Breadth First Search
Breadth First Search
Breadth First Search
Breadth First Search
Breadth First Search
Breadth First Search
Time and Space Complexity

Using Adjacency List


Time Complexity: O(V+E), where V is the number of nodes and E is the
number of edges.
Space Comolexity: O(V)
Time Complexity: O(|V|^2), |E|=|V|^2 where V is the number of nodes and E
is the number of edges.
Space Comolexity: O(|V|^2)
Application of BFS
Application of BFS
Application of BFS
DFS

The depth-first search (DFS) algorithm starts with the initial node of graph G
and goes deeper until we find the goal node or the node with no children.

Because of the recursive nature, stack data structure can be used to


implement the DFS algorithm.
DFS

The step by step process to implement the DFS traversal is given as follows -

1. First, create a stack with the total number of vertices in the graph.
2. Now, choose any vertex as the starting point of traversal, and push that
vertex into the stack.
3. After that, push a non-visited vertex (adjacent to the vertex on the top of the
stack) to the top of the stack.
4. Now, repeat steps 3 and 4 until no vertices are left to visit from the vertex on
the stack's top.
5. If no vertex is left, go back and pop a vertex from the stack.
6. Repeat steps 2, 3, and 4 until the stack is empty.
DFS
DFS

Step 1 - First, push H onto the stack.

STACK: H
Step 2 - POP the top element from the stack, i.e., H, and print it. Now, PUSH
all the neighbors of H onto the stack hat are in ready state.

Print: H
STACK: A
DFS

Step 3 - POP the top element from the stack, i.e., A, and print it. Now, PUSH
all the neighbors of A onto the stack

Print: A
STACK: B, D
Step 4 - POP the top element from the stack, i.e., D, and print it. Now, PUSH
all the neighbors of D onto the stack
Print: D
STACK: B, F
DFS

Step 5 - POP the top element from the stack, i.e., F, and print it. Now, PUSH
all the neighbors of F onto the stack

Print: F
STACK: B
Step 6 - POP the top element from the stack, i.e., B, and print it. Now, PUSH
all the neighbors of B onto the stack
DFS

Step 5 - POP the top element from the stack, i.e., F, and print it. Now, PUSH
all the neighbors of F onto the stack

Print: F
STACK: B
Step 6 - POP the top element from the stack, i.e., B, and print it. Now, PUSH
all the neighbors of B onto the stack
Print: B
STACK: C
DFS

Step 7 - POP the top element from the stack, i.e., C, and print it. Now, PUSH
all the neighbors of C onto the stack

Print: C
STACK: E, G
Step 8 - POP the top element from the stack, i.e., G and PUSH all the
neighbors of G onto the stack
Print: G
STACK: E
DFS

Step 9 - POP the top element from the stack, i.e., E and PUSH all the
neighbors of E onto the stack

Print: E
STACK:
Now, all the graph nodes have been traversed, and the stack is empty.
Time and Space Complexity of DFS

Using Adjacency List


Time Complexity: O(V+E), where V is the number of nodes and E is the
number of edges.
Space Comolexity: O(V)
Time Complexity: O(|V|^2), |E|=|V|^2 where V is the number of nodes and E
is the number of edges.
Space Comolexity: O(|V|^2)
Applications of Depth First Search:

1. Detecting cycle in a graph: A graph has a 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.

2. Path Finding: We can specialize the DFS algorithm to find a path between
two given vertices u and z.

Call DFS(G, u) with u as the start vertex.


Use a stack S to keep track of the path between the start vertex and the current
vertex.
As soon as destination vertex z is encountered, return the path as the contents
of the stack
Applications of Depth First Search:

3. Topological Sorting: Topological Sorting is mainly used for scheduling jobs from the
given dependencies among jobs. In computer science, applications of this type arise in
instruction scheduling, ordering of formula cell evaluation when recomputing formula
values in spreadsheets, logic synthesis, determining the order of compilation tasks to
perform in makefiles, data serialization, and resolving symbol dependencies in linkers.
4. To test if a graph is bipartite: We can augment either BFS or DFS when we first
discover a new vertex, color it opposite 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.
(See this for DFS-based algo for finding Strongly Connected Components)
Applications of Depth First Search:

6. Solving puzzles with only one solution: such as mazes. (DFS can be adapted
to find all solutions to a maze by only including nodes on the current path in the
visited set.).
7. Web crawlers: Depth-first search can be used in the implementation of web
crawlers to explore the links on a website.
8. Maze generation: Depth-first search can be used to generate random mazes.
9. Model checking: Depth-first search can be used in model checking, which is
the process of checking that a model of a system meets a certain set of
properties.
10. Backtracking: Depth-first search can be used in backtracking algorithms.
Bipartite Graph

A Bipartite Graph is a graph whose vertices can be divided into two


independent sets, U and V such that every edge (u, v) either connects a
vertex from U to V or a vertex from V to U.
Bipartite Graph

In simple words, a graph is said to be a Bipartite Graph if we are able to


colour the graph using 2 colours such that no adjacent nodes have the same
colour.
Bipartite Graph

If the graph has an odd length cycle, it is not a bipartite graph.

Two vertices of the same set will be connected, which contradicts the
Bipartite definition saying there are two sets of vertices and no vertex will be
connected with any other vertex of the same set.
Bipartite Graph
Bipartite Graph
Bipartite Graph
Bipartite Graph
Bipartite Graph
Bipartite Graph
Bipartite Graph
Bipartite Graph
Bipartite Graph
Bipartite Graph
Not a Bipartite Graph
Not a Bipartite Graph
Not a Bipartite Graph
Not a Bipartite Graph
Not a Bipartite Graph
Not a Bipartite Graph
Not a Bipartite Graph
Time and Space Complexity

We are traversing through all the nodes and edges. So time complexity will
be O(V + E) where V = vertices or node, E = edges.
We use a coloured array, queue, and an adjacency list for the graph. So the
space complexity will be O(V) + O(V) + O(V + E).
Graph Coloring

Graph coloring refers to the problem of coloring vertices of a graph in such a


way that no two adjacent vertices have the same color. This is also called
the vertex coloring problem. If coloring is done using at most m colors, it is
called m-coloring.
Chromatic Number

The minimum number of colors needed to color a graph is called its


chromatic number. For example, the following can be colored a minimum of
2 colors.
Chromatic Number

The minimum number of colors needed to color a graph is called its


chromatic number. For example, the following can be colored a minimum of
2 colors.
Graph Coloring

Graph coloring problem is both, a decision problem as well as an


optimization problem.

• A decision problem is stated as, “With given M colors and graph G,


whether a such color scheme is possible or not?”.
• The optimization problem is stated as, “Given M colors and graph G, find
the minimum number of colors required for graph coloring.”
Approach 1: Brute Force

The simplest approach to solve this problem would be to generate all


possible combinations (or configurations) of colours.
After generating a configuration, check if the adjacent vertices have the
same colour or not. If the conditions are met, add the combination to the
result and break the loop.
Since each node can be coloured by using any of the M colours, the total
number of possible colour configurations are mV. The complexity is
exponential which is very huge.
Approach 1: Brute Force
Approach 1: Brute Force

Time Complexity: O(M^V), where M is the total colours needed and V is the
total vertices
Space Complexity: O(V), as extra space is used for colouring vertices.
Approach 2: Backtracking

In this approach, the idea is to color a vertex and while coloring any adjacent
vertex, choose a different color. Similarly, color every possible vertex
following the restrictions, till any further vertex is left coloring. In any case, if
all adjacent vertices for a given vertex are colored, then backtrack and
change color.
Approach 2: Backtracking
Approach 2: Backtracking

Algorithm

• Consider a color and check if it is valid i.e. from the given vertex check
whether its adjacent vertices have been coloured with the same color.
• If true, pick a different colour, else continue colouring the vertices.
• If no other color is left un-used, then backtrack.
Time Complexity: O(M^V), in the worst case.
Space Complexity: O(V), as extra space is used for colouring vertices.
What is Hamiltonian Cycle?

Hamiltonian Cycle or Circuit in a graph G is a cycle that visits every


vertex of G exactly once and returns to the starting vertex.
• If graph contains a Hamiltonian cycle, it is called Hamiltonian graph
otherwise it is non-Hamiltonian.
• Finding a Hamiltonian Cycle in a graph is a well-known NP-complete
problem, which means that there’s no known efficient algorithm to
solve it for all types of graphs. However, it can be solved for small or
specific types of graphs.
• The Hamiltonian Cycle problem has practical applications in various
fields, such as logistics, network design, and computer science.
Hamiltonian Cycle using Backtracking Algorithm

Algorithm
• Create an empty path array and add vertex 0 to it.
• Start adding other vertices by checking if they have been added
previously or not.
• We can check this by creating a visiting array to check if the vertex
has already been visited or is adjacent to the previously added
vertex.
• If any such vertex is found, add it to the path array and backtrack
from there.
• If no such vertex is found we return False.
Hamiltonian Cycle using Backtracking Algorithm

Start with the node 0 .


Apply DFS for finding the Hamiltonian
path.
When base case reach (i.e. total no of
node traversed == V (total vertex)):
Check weather current node is a
neighbour of starting node.
As node 2 and node 0 are not
neighbours of each other so return
from it.
Hamiltonian Cycle using Backtracking Algorithm

Step 1:
Tour is
started
from vertex
1. There is
no path
from 5 to 1.
So it’s the
dead-end
state
Hamiltonian Cycle using Backtracking Algorithm

Step 2:
Backtrack to the
node from where
the new path
can be explored,
that is 3 here
Hamiltonian Cycle using Backtracking Algorithm

Step 3: New
path also leads
to a dead end so
backtrack and
explore all
possible paths
Hamiltonian Cycle using Backtracking Algorithm

Step 4: Next
path is also
leading to a
dead-end so
keep
backtracking
until we get
some node that
can generate a
new path, i.e
.vertex 2 here
Hamiltonian Cycle using Backtracking Algorithm

Step 5: One
path leads to
Hamiltonian
cycle, next leads
to a dead end so
backtrack and
explore all
possible paths at
each vertex
Hamiltonian Cycle using Backtracking Algorithm

Step 6: Total two


Hamiltonian
cycles are
detected in a
given graph
Hamiltonian Cycle using Backtracking Algorithm

• Time Complexity : O(N!), where N is number of vertices.


• Auxiliary Space : O(1), since no extra space used.
Hamiltonian Cycle using Backtracking Algorithm
Sum of Subsets Problem

Sum of Subsets Problem: Given a set of positive integers, find the


combination of numbers that sum to given value M.

Example 1:
Input: set[] = {4, 16, 5, 23, 12}, sum = 9
Output = true
Subset {4, 5} has the sum equal to 9.
Sum of Subsets Problem

Example 2:
Input: set[] = {2, 3, 5, 6, 8, 10}, sum = 10
Output = true
There are three possible subsets that have the sum equal to 10.
Subset1: {5, 2, 3}
Subset2: {2, 8}
Subset3: {10}
Sum of Subsets Problem

There are two ways of solving the subset problem:

• Recursion/Backtracking
• Dynamic programming
Sum of Subsets Problem

n = 4, Sum = 13, and w1 = 3, w2 = 4, w3 = 5 and w4 = 6. Find a solution to


the problem using backtracking.
Set = {3,4,5,6} Set Should be sorted in assending order
Sum=13
Ans Set X = [0, 0, 0, 0], X is an array of four items.
Set Sum = 0. Sum indicates summation of selected numbers from W.
Sum of Subsets Problem

Step 1 : i = 1, Adding item wi Step 2 : i = 2, Adding item w2

Sum = Sum + wi = Sum + w2 = 3 + 4 =


Sum = Sum + wi = Sum + w1 = 0 + 3 7
=3
Sum ≤ M, so add item i to solution set.
Sum ≤ M, so add item i to solution
set. X[i] = X[2] = 1 ⇒ X =[1, 1, 0, 0]

X[i] = X[1] = 1 ⇒ X =[1, 0, 0, 0]


Sum of Subsets Problem

Step 3 : i = 3, Adding item w3 Step 4 : i = 4, Adding item w4

Sum = Sum + wi = Sum + w4 = 12 + 6 = 18


Sum = Sum + wi = Sum + w3 = 7 + 5 =
12 Sum > M, so backtrack and remove the
previously added item from the solution
set.
Sum ≤ M, so add item i to solution set.
X[i] = X[3] = 0 ⇒ X =[1, 1, 0, 0].

X[i] = X[3] = 1 ⇒ X =[1, 1, 1, 0] Update Sum accordingly. So, Sum = Sum


– w3 = 12 – 5 = 7

And don’t increment i.


Sum of Subsets Problem

Step 5 : i = 4, Adding item w4

Sum = Sum + wi = Sum + w4 = 7 + 6


= 13

Sum = M, so solution is found and


add item i to solution set.

X[i] = X[4] = 1 ⇒ X =[1, 1, 0, 1]


Sum of Subsets Problem
At level i, the left branch
corresponds to the inclusion
A complete state space tree of number wi and the right
branch corresponds to
exclusion of number wi.
Sum of Subsets Problem

You might also like