Professional Documents
Culture Documents
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:
Bi-connected Graph
Traversing Trees:
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.
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.
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):
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:
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:
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
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.)
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:
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:
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.
ABC
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
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:
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
Antony Gardening
Successor State
Terminal State
Utility
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