You are on page 1of 77

Unit-5

Exploring Graphs
Prepared By: Bhavini Tandel
Articulation Point

 If there is any vertex whose removal will disconnect the graph into multiple
components than that vertex is called articulation point.

1 1

4 2 4 2

5 6
5 6
 If vertex 3 is removed from graph than vertex 1, 4, 2 form one component
and vertex 5 and 6 form another component.
 So, here vertex 3 is articulation point.
 If a graph representing some network, like some road network or computer
network. So there we don’t want any articulation point.
 For disconnected undirected graph, an articulation point is a vertex
removing which increases number of connected components.
 If that articulation point is having any problem than the network will break
into multiple points.
 Solution is if we join the edge between vertex 4 and 5 than there is no
articulation point in a graph.
 If we remove vertex 3 from the graph vertex 5 and 6 will connected via
vertex 4 and if vertex 4 is also removed than the probability multiple node
are failing is less.
 So, if vertex 3 is remove than the graph will not break it to multiple pieces.
So there is no articulation point.
1

4 2

5 6
Remove Articulation point from graph
Bi-connected Graph:

 A graph is said to be bi-connected if


 It is connected i.e. it is possible to reach every vertex from every other
vertex by a simple path.
 Even after removing any vertex the graph remains connected.
 The graph is connected and when try to remove the vertices one by one
and observe that removing of the vertices does not increase the number of
connected components. So, the graph is called bi-connected.

Bi-connected Graph
Traversing Trees:

 Traversal is a process to visit all the nodes of a tree.


 Because all nodes are connected via edges we always start from the root
node. That is, we cannot randomly access a node in a tree.
 There are 3 ways which we use to traverse a tree.
(1) In-Order Traversal
(2) Pre-Order Traversal
(3) Post-Order Traversal
 In-Order Traversal (Left-Root-Right):
 In this traversal method, the left sub tree is visited first then the root and
later the right sub tree.

Left Sub Tree Right Sub Tree

In-Order Traversal: D – B – E – A – F – C - G
 Pre-Order Traversal (Root-Left-Right):
 In this traversal method, the root node is visited first then the left sub tree
and finally the right sub tree.

Left Sub Tree Right Sub Tree

Pre-Order Traversal: A – B – D – E – C – F - G
 Post-Order Traversal (Left-Right-Root):
 In this traversal method, the root node is visited last, first we traverse the
left sub tree then the right sub tree and finally the root node.

Left Sub Tree Right Sub Tree

Post-Order Traversal: D – E – B – F – G – C - A
Graph Traversals:

 Graph traversal means visiting every vertex and edge exactly once in a
well defined order. While using certain algorithms, you must ensure that
each vertex of the graph is visited exactly once.
 Traversing the graph means examining all the nodes/vertices of the graph.
There are two standard methods by using which we can traverse the
graphs.
1) Breadth first Search (BFS)
2) Depth First Search (DFS)
 For traversal we have to familiar with two terms,
(1) Visiting a vertex: Going on a particular vertex.
(2) Exploration of vertex: If you are on a particular vertex than visiting all its
adjacent vertices.
Breadth First Search (BFS):

 BFS is most commonly used approach.


 This algorithm selects a single vertex i.e. source vertex or any vertex in a
graph and then all of the adjacent vertices are visited one by one.
 After completing all of the adjacent vertices, it moves further to check
another vertex and checks its adjacent vertices again.
 To implement this algorithm, we need to use the queue data structure.
 All the adjacent vertices are added into queue when all adjacent vertices
are completed, one item is removed from the queue and start traversing
through that vertex again.
 As the name suggest, you are required to traverse the graph breadthwise
as follows:
 First move horizontally and visit all the nodes of the current layer
 Than move to the next layer
 Example:

1 1 1

2 3 2 3 2 3

4 5 4 5 4 5
1 1 1

2 3 2 3 2 3

4 5 4 5 4 5
 Things to remember for BFS:
 You can start BFS from any vertex you like
 When you are exploring any vertex than you can explore adjacent
vertices in any order.
 Rule is when you are selecting a vertex for exploration you must visit all
its adjacent vertices than only you should go to next vertex for
exploration.
 You should select the next vertex for exploration from a queue only.
 Time Complexity:
 Time complexity of Breadth First Search is O(V+E) where,
 V is number of vertices in the graph and
 E is number of edges in the graph
Advantages and Disadvantages of BFS:

 Advantages:
 Used to find the shortest path between vertices
 Always finds optimal solutions.
 There is nothing like useless path in BFS, since it searches level by level.
 Finds the closest goal in less time
 Disadvantages:
 All of the connected vertices must be stored in memory. So consumes
more memory
Algorithm:
Application of BFS:

 Shortest path and minimum spanning tree for unweighted graph


 Crawlers in search engines
 Social networking websites
 GPS navigation systems
 Broadcasting in network
 Path finding
 Cycle detection in undirected graph
Depth First Search (DFS):

 DFS is an algorithm for finding or traversing graphs or trees in depth-ward


direction.
 Depth first search algorithm starts with the initial vertex of the graph G and
then goes to deeper and deeper until we find the goal node or the node
which has no children.
 Then the algorithm backtracks from the dead end towards the most recent
node that is yet to be completely unexplored.
 The data structure which is being used in DFS is stack.
 The process is similar to BFS algorithm. In DFS, the edges that leads to an
unvisited vertex are called discovery edges while the edges that leads to
an already visited node are called back edges
 DFS algorithm is a recursive algorithm that uses the idea of backtracking.
 Backtracking:
 When you are moving forward and there are no more nodes along the
current path then you have to move backwards on the same path to
find nodes to traverse.
 DFS algorithm works as follow:
 Start by putting only one of the graph’s vertices on top of a stack.
 Take the top item of the stack and add it to the visited list.
 Create a list of that vertex’s adjacent nodes. Add the nodes which are
not in the visited list to the top of the stack.
 Keep repeating these steps until the stack is empty.
 Example:

S S

A B C A B C
S Top of the Stack

Stack
Stack
D D
S S

D
A B C A B C A
A
S
S
Stack
Stack
D D
S S

B
D D
A B C A B C A
A
S S

Stack Stack
D D
S

C
D
A B C
A
S
Stack
D

 As C does not have any unvisited adjacent node so we keep popping the
stack until we find a node that has an unvisited adjacent node.
 In this case, there is none and we keep popping until the stack is empty.
 Time Complexity:
 Time complexity of Depth First Search is O(V+E) where,
 V is number of vertices in the graph and
 E is number of edges in the graph
Advantages and Disadvantages of DFS:

 Advantages:
 Consumes less memory
 Finds the larger distant element(from source vertex) in less time.
 Disadvantages:
 May not find optimal solution to the problem.
 May get trapped in searching useless path.
Application of DFS:

 Detecting cycle in a graph


 Topological Sorting
 To test if a graph is bipartite
 Solving puzzles with only one solution (Ex. Maze)
Algorithm:
Difference between BFS and DFS:

BFS DFS
It finds the shortest path to the DFS goes to the bottom of a sub tree
destination. then backtrack.
It uses a queue to keep track of the It uses a stack to keep track of the
next location to visit. next location to visit.
BFS traverses according to tree level. DFS traverses according to tree depth.
It is implemented using FIFO list. It is implemented using LIFO list.
There is no need to backtracking in There is a need of backtracking in
BFS. DFS.
You can never be trapped into finite You can be trapped into infinite loops.
loops
Topological Sort

 Topological sorting for Directed Acyclic Graph (DAG) is a linear ordering of


vertices such that for every directed edge u v, vertex u comes before v in
the ordering.
 Topological Sorting for a graph is not possible if the graph is not a DAG.
 There are multiple topological sorting possible for a graph.
 Example:

A B C

E F

D
 Find in-degree for every vertex. (In-degree: The number of edges coming
into a vertex in a directed graph.)

 Find a node with an in-degree of zero and add it to the topological


ordering. Once a node is added to the topological ordering, we can take
the node and its outgoing edges out of the graph.
A B C

E F

Topological Ordering: A, B
B C

E F

Topological Ordering: A, B, C
C

E F

Topological Ordering: A, B, C, D
E F

Topological Ordering: A, B, C, D, E
E F

Topological Ordering: A, B, C, D, E, F
Algorithm:
 Time Complexity:
 O(V+E)
 Space Complexity:
 O(V)
 Application:
 A topological sort is useful in something like a course prerequisite
system, where you know what classes you need to take before you can
take others
 Instruction scheduling
 planning and scheduling
 Building houses
Backtracking:

 A backtracking algorithm is a problem-solving algorithm that uses a brute


force approach for finding the desired output.
 The Brute force approach tries out all the possible solutions and chooses the
desired/best solutions.
 The term backtracking says that if the current solution is not suitable then
backtrack and try other solutions. Thus, recursion is used in this approach.
 This approach is used to solve problems that have multiple solutions. If you
want an optimal solution than you must go for dynamic programming.
 In dynamic programming, we solve optimization problems but
backtracking is not for optimization problem.
 Backtracking is used when you have multiple solutions and you want all
those solutions.
 Example:
 You have 3 students, 2 boys and 1 girl and there are 3 chairs. You have
to arranged them on those three chairs.
 So, how many ways we can arrange those students?

There are total 3 students


Total possible ways are 3! = 6
 We want to find out all possible arrangements. So, for finding all possible
arrangements we can solve the problem and solution can be
represented in a form of tree or solution tree. This solution tree is called
as state space tree.

B1 G1
B2

B2 G1 B1 G1 B1 B2

G1 B2 G1 B1 B2 B1
 The problem that we are solving using backtracking will have some
constraint and so we have to get solution which are satisfying the
constraint.
 Constraint: Girl should not seat in between. So the possible solutions are:

B1 G1
B2

B2 G1 B1 G1 B1 B2

G1 G1 B2 B1

Not allowed so no need to go further. We are killing these nodes and it is called bounding function.
 Brute force approach is also used by branch and bound. So, the difference
between backtracking and branch and bound is,
 Both follows brute force approach and both generate state space tree.
 Backtracking follows DFS and Branch and Bound follows BFS.
 Application of Backtracking:
 To find all Hamiltonian path present in a graph.
 To solve the N queen’s problem.
 Maze solving problem.
Knapsack Problem:

 The knapsack can carry a weight not exceeding W. Our aim is to fill the
knapsack in a way that maximize the value of the included objects, while
respecting the capacity constraint.
 Here, we can’t take fraction.
 Example:
 Here, take W=8

p
 Initially the partial solution is empty. The backtracking algorithm explores the
tree as in a depth first search, constructing nodes and partial solutions as it
goes.
 So, here the optimal solution is (3 5; 15).
 Selected Objects are, (0, 1, 0, 1) with profit 5+10=15.
Algorithm:
Eight Queen’s Problem:

 The N queen problem is to place N queens in such a manner on a N * N


chessboard that no queens attack each other by being in the same row,
column or diagonal.
 Strategy: The rows and columns are numbered through 1 to 8.
 The queens are also numbered through 1 to 8.
 For the N queen’s problem we used backtracking.
 In N-Queen’s problem, we have to place 4 queens such as q1 q2 q3 and
q4 on the chessboard such that no two queens attack each other. In such
a conditional each queen must be placed on a different row, i.e., we put
queen "i" on row "i“.
 So, the state space tree for 4 queen’s problem is as follow:

X1=1 2

4
X2=2 X2=1 4
3 3

X3=2 4 2 3
X3=1

3 X4=3
Solution of 4 Queen’s problem Other possible Solution of 4 Queen’s problem
(2, 4, 1, 3) (3, 1, 4, 2)
 We can solve this problem in the same way as in 4 queens.
 For 8-queens, generally 92 solutions are possible excluding symmetry only 12
unique solutions exist.

The solution for 8 -queen problem: (4, 6, 8, 2, 7, 1, 3, 5).


 Time Complexity:
 O(n!)
Branch and Bound:

ABC

Arts Science Commerce

Engineering Medical Dental Law

Homeopathy Allopathy Ayurvedic

Surgery Pathology Anaesthesia General Medicine


 Branch and bound is an algorithm design paradigm which is generally used
for solving combinatorial optimization problems.
 Branch and bound is a technique for exploring an implicit directed graph.
 The graph is usually acyclic or a tree.
 This technique is used to solve a problem in optimized way, it will give the
optimal solution of the given problem.
 At each node we calculate a bound on the possible value of any solution
that may lie on the graph.
 Calculated bound is used to choose which open path looks the most
promising. so, it can be explored first.
 A BFS search explores nodes in the order of their creation using a queue.
Branch and bound uses BFS technique to solve a problem.
 The branch and bound algorithm technique solves problems relatively
quickly.
 It is used to solve optimization problem and only minimization problem not
maximization problem. Anyway we can convert maximization problem to
minimization problem and we can solve it.
0/1 Knapsack Problem:

 Objective is to fill the bag with those objects such that the total weights of
the objects included in the bag should be less than or equal to the
capacity.
 If you are taking any object than you must take the complete object or
don't take it at all.
 So, the problem is total profit gain should be maximized. So, it's a
maximization problem.
 0/1 knapsack problem can be solve using greedy method, dymanic
programming and even using backtracking.
 Here in branch and bound, We only solve the minimization problem but in
knapsack problem we want answer is maximum. So, we can convert the
sign of these profits instead of positive will make it as negative weights and
negative profit and solve the problem.
 So, maximization problem can be converted to minimization and solve the
problem and again convert result back to positive.
 Here, LC branch and bound method is used and explore all the nodes
whose cost is minimum.
 For solving this algorithm using branch and bound for each node in a state
space tree we have to find out upper bound and cost.
 Upper bound u = σ𝒏𝒊=𝟏 𝒑𝒊𝒙𝒊 ≤m (take without fraction)
 Cost c = σ𝒏𝒊=𝟏 𝒑𝒊𝒙𝒊 (take with fraction)
 Though the problem didn’t allow to take fraction but only for calculation
fraction is used. Fraction is not included in the solution.
Upper=-32

U=-32
1 C=-38

U=-32 U=-22
C=-38 2 3 C=-32
Killed

U=-32 U=-22
C=-38 4 5
C=-36
Killed

U=-32 U=-38
6 7 C=-38
C=-38
Not feasible
 If you get the smaller upper bound value than check other alive nodes, if
the cost of other nodes are greater then killed all those nodes.
 No need to explore those nodes because time will be wasted in
exploration.
Upper=-38
U=-32
C=-38
1

Solution is: x {1,1, 0,1}


U=-32 U=-22
C=-38 2 3 C=-32
Killed

U=-32 U=-22
C=-38 4 5
C=-36
Killed
U=-38
U=-32 C=-38
6 7
C=-38
Not feasible
U=-38 U=-20
8 9
C=-38 C=-20
Assignment Problem:

 There are n workers and n jobs in this problem.


 The assignment should be done in such a way that every worker has
exactly one job and cost is assigned to each job.
 We need to minimize the total cost of assignment.
 Example: There are 3 jobs like Cooking, Gardening and Cleaning.

Cooking Gardening Cleaning


Amar 2 6 7
Akbar 4 8 3
Anthony 9 5 1

Here assignment should be done to minimize the total time.


Solved using Brute Force Method

11 10 11 16 18 24
 Here in branch and bound, the state space tree is generated to find the
solution in following manner:
 Step 1: Identify the lowest bound by taking minimum value from each
row.
 Step 2: Generate the state space tree.
 Step 3: Find the total cost.
LB = 6

Amar Cooking Amar Gardening Amar Cleaning


2+5+1=8 6+4+1=11 7+4+5=16

Akbar Gardening Akbar Cleaning


8+2+1=11 3+2+5=10

Antony Gardening

Total Cost = 2+3+5 = 10


Assign Cooking to Amar, Cleaning to Akbar and Gardening to Antony
Minmax Problem:

 Minmax is a recurssive algorithm which is used to choose an optimal move


for a player assuming that the other player is also playing optimally.
 It is used in games such as tic-tac-toe, chess, checkers and many other two
player games.
 There can be two player games which are not of perfect information such
as scrabble because the opponent’s move cannot be predicted.
 It is similar to how game is played: “if I make this move then my opponent
can only make these moves” and so on.
Initial State

Successor State

Terminal State
Utility

Game tree for Tic-Tac-Toe


 How does the algorithm work?
 There are two players involved in a game called MIN and MAX. The
player MAX tries to get the highest possible score and MIN tries to get
the lowest possible score. i.e. MIN and MAX try to act opposite of each
other.
 General process of the Minmax algorithm is as follows:
 Step 1: generate the entire game tree starting with the current
position of the game all the way up to the terminal states. The figure
shows how the game tree looks like for tic-tac-toe.
 Step 2: Apply utility function to get the utility values for all the
terminal states.
MAX 3

MIN 3 2

TERMINAL 3 5 10 2 2 3 5 10 2 2

MINMAX Algorithm
 Step 3: Determines the utilities of the higher nodes with help of the
utilities of the terminal nodes.
 Step 4: Calculate the utility values with the help of leaves considering
one layer at a time until the root of the tree.
 Step 5: All the backed up values reaches to the root of the tree. i.e. the
topmost point. At that point MAX has to choose the highest value.
 To summarize,
 Minmax Decision = MAX{ MIN{3, 5, 10}, MIN{2,2} }
= MAX{3, 2}
=3
 Alpha-Beta Pruning:
 It is not actually a new algorithm rather an optimization technique for
Minmax algorithm and it reduces the computation time.
 It allows to search much faster and even go into deeper levels in the
game tree. It cuts off branches in the game tree which need not be
searched because there already exists a better move available and It is
called alpha beta pruning.
MAX 3

MIN ≤3 ≤-4

MAX 3 ≥5 -4

MIN -1 3 5 ? -6 -4

Alpha – Beta Pruning

You might also like