Algorithm 12th Graph Algorithm

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 93

Design and Analysis of

Algorithms

Greedy Approach?
 The greedy approach suggests constructing
a solution through a sequence of steps, each
expanding a partially constructed solution
obtained so far, until a complete solution to
the problem is reached
 On each step a choice made must be:
 feasible, i.e., it has to satisfy the problem’s
constraints
 locally optimal, i.e., it has to be the best
local choice among all feasible choices avail
 irrevocable, i.e., once made, it cannot be
changed on subsequent steps of the
algorithmable on that step
Minimum Spanning Tree
 A Spanning Tree for a connected, undirected
graph, G = (V, E), is a subgraph of G that is an
undirected tree and contains all the vertices of G.
 In a weighted graph G = (V, E, W), the weight of a
subgraph is the sum of the weights of the edges in
the subgraph.
 A minimum spanning tree (MST) for a weighted
graph is a spanning tree with minimum weight.
Minimum Spanning Tree
 Minimum spanning trees are useful when we want
to find the cheapest way to connect a

– Set of cities by roads

– Set of electrical terminals or computers by wires or


telephone lines

– Etc…
Graph
2.0
 Consider the following graph A B
 G = { V, E, W } 3.0 2.0 1.0

 Vertices (V) = A,B,C,D D


C
 Edges (E) = {(A,B),(B,D),(D,C), 4.0

(C,A),(D,A)}
Weight (W) = {2,1,4,3,2}
Minimum Spanning Tree
1. Tree can’t have a cycle.
2. Some of the possible spanning
trees for this graph are:
2.0
In Tree: A B
2.0 1.0
V = Graph Vertices. (A,B,C,D) 3.0

E = Edges = V -1 C D
4.0
2.0 2.0
A B A B A B
2.0 2.0
3.0 1.0 3.0 1.0 3.0
C D C D D
C
MST Weight is 6 MST Weight is 6 Weight is 7
Minimum Spanning Tree
1. To find the minimum cost spanning tree, two
technique are used.
1. Prim’s Algorithm
2. Kruskals Algorithm
Minimum Spanning Tree
2
A B
3 6
7
4
G
1 3
5 4 2
F I H C
2 8
6 1 2
E D
Minimum Spanning Tree
2
A B
3 6
7
4
G
1 3
5 4 2
F I H C
Prim’s Algorithm for Minimum
Spanning Tree
 Prim’s algorithm begins by selecting an arbitrary starting
vertex, and then “branches out” form the past of the tree
constructed so far by choosing a new vertex and edge at
each iteration.
 The new edge connects the new vertex to the previous tree.
 Prim’s algorithm always chooses an edge of minimum
weight from a tree vertex to a fringe vertex.
Prim’s Algorithm for Minimum
Spanning Tree
 The key step in the algorithm is the
selection of a vertex from the fringe and an
incident edge.
 Prim’s algorithm always chooses an edge of
minimum weight from a tree vertex to a
fringe vertex.
 The general algorithm structure is
Prim’s Algorithm for Minimum
Spanning Tree
Fringe Vertices
A 2 B
3 6 The tree so far 2
7 B
G 4
1 3 A 3
G
F 5 I 4 H 2 C
7
2 8 F
6 1 2
E D
The tree and fringe after the
starting vertex A is selected
Prims’s Algorithm for Minimum
Spanning Tree
Fringe Vertices
A 2 B
3 6 The tree so far 4
7 C
G 4
2 B 3
1 3 G
F 5 I 4 H 2 C A
7
2 8 F
6 1 2
E D
After Selecting an edge and vertex: BG
is not shown because AG is a better
choice to reach G.
Prims’s Algorithm for Minimum
Spanning Tree
Fringe Vertices
A 2 B
3 6 The tree so far 4
7 C
G 4
2 B
1 3 1 I
3 G 3
F 5 I 4 H 2 C A H
7
2 8 F
6 1 2
E D
After Selecting an edge AG : GB is not
shown because vertex B is already
include in a tree.
Prims’s Algorithm for Minimum
Spanning Tree
A 2 B The final Minimum Spanning tree
3 6 after prim’s algorithm is
7 4
G
1 3 2 B
3 G 1 I 2 E 1 D
F 5 I 4 H 2 C A
5 2
2 8 F C
6 1 2
E D 2
H
Example
2
2: Prim's Algorithm
A B 1
1
2 5 E
1
1 D
C 5 3
6 G vertex cost prev
10
2 A
F
B
C
D
E
F
G
CSE 332 Data Abstractions, Summer
August 13, 2012 17
2012
Example
2
2: Prim's Algorithm
A B 1
1
2 5 E
1
1 D
C 5 3
6 G vertex cost prev
10
2 A - -
F
B 2 A
C 2 A
Show the vertices of
D 1 A
starting point ‘A’
E
F
G
CSE 332 Data Abstractions, Summer
August 13, 2012 18
2012
Example
2
2: Prim's Algorithm
A B 1
1
2 5 E
1
1 D
C 5 3
6 G vertex cost prev
10
2 A - -
F
B 2 A
C 2 A
Show the vertices of
D 1 A
starting point ‘A’
E
F
G
CSE 332 Data Abstractions, Summer
August 13, 2012 19
2012
Example
2
2: Prim's Algorithm
A B 1
1
2 5 E
1
1 D
C 5 3
6 G vertex cost prev
10
2 A
F
B 2 A
C 2 1 A D
Show the vertices of
D 1 A
point ‘A’ and ‘D’
E 1 D
F 6 D
G 5 D
CSE 332 Data Abstractions, Summer
August 13, 2012 20
2012
Example
2
2: Prim's Algorithm
A B 1
1
2 5 E
1
1 D
C 5 3
6 G vertex cost prev
10
2 A
F
B 2 A
C 1 D
Show the vertices of
D 1 A
point ‘A’ and ‘D’
E 1 D
F 6 D
G 5 D
CSE 332 Data Abstractions, Summer
August 13, 2012 21
2012
Example
2
2: Prim's Algorithm
A B 1
1
2 5 E
1
1 D
C 5 3
6 G vertex cost prev
10
2 A
F
B 2 A
C 1 D
Show the vertices of
D 1 A
point ‘A’ ,‘D’ and ‘C’
E 1 D
F 6 2 D C
G 5 D
CSE 332 Data Abstractions, Summer
August 13, 2012 22
2012
Example
2
2: Prim's Algorithm
A B 1
1
2 5 E
1
1 D
C 5 3
6 G vertex cost prev
10
2 A
F
B 2 A
C 1 D
Show the vertices of
D 1 A
point ‘A’ ,‘D’ and ‘C’
E 1 D
F 2 C
G 5 D
CSE 332 Data Abstractions, Summer
August 13, 2012 23
2012
Example
2
2: Prim's Algorithm
A B 1
1
2 5 E
1
1 D
C 5 3
6 G vertex cost prev
10
2 A
F
B 2 1 A E
C 1 D
Show the vertices of
D 1 A
point ‘A’ ,‘D’ , ‘C’ and
E 1 D
‘E’
F 2 C
G 5 3 D E
CSE 332 Data Abstractions, Summer
August 13, 2012 24
2012
Example
2
2: Prim's Algorithm
A B 1
1
2 5 E
1
1 D
C 5 3
6 G vertex cost prev
10
2 A
F
B 1 E
C 1 D
Show the vertices of
D 1 A
point ‘A’ ,‘D’ , ‘C’ and
E 1 D
‘E’
F 2 C
G 3 E
CSE 332 Data Abstractions, Summer
August 13, 2012 25
2012
Example
2
2: Prim's Algorithm
A B 1
1
2 5 E
1
1 D
C 5 3
6 G vertex cost prev
10
2 A
F
B 1 E
C 1 D
Show the vertices of
D 1 A
point ‘A’ ,‘D’ , ‘C’ ,
E 1 D
‘E’ and ‘B’
F 2 C
G 3 E
CSE 332 Data Abstractions, Summer
August 13, 2012 26
2012
Example
2
2: Prim's Algorithm
A B 1
1
2 5 E
1
1 D
C 5 3
6 G vertex cost prev
10
2 A
F
B 1 E
C 1 D
Show the vertices of
D 1 A
point ‘A’ ,‘D’ , ‘C’ ,
E 1 D
‘E’ and ‘B’
F 2 C
G 3 E
CSE 332 Data Abstractions, Summer
August 13, 2012 27
2012
Example
2
2: Prim's Algorithm
A B 1
1
2 5 E
1
1 D
C 5 3
6 G vertex cost prev
10
2 A
F
B 1 E
Output: C 1 D
(A, D) (C, F)
D 1 A
(B, E) (D, E)
(C, D) (E, G) E 1 D
F 2 C
Total Cost: 9 G 3 E
CSE 332 Data Abstractions, Summer
August 13, 2012 28
2012
Prim’s Algorithm for Minimum
Spanning Tree
Prim’s Algorithm for Minimum
Spanning Tree
The time complexity of Prim's algorithm depends
on the data structures used for the graph and for
ordering the edges by weight.

O(n^2) or O(|E| log |V|)


Kruskal’s Algorithm for
Minimum Spanning Tree
Kruskal’s Algorithm
Make a disjoint set for each vertex

1 2 {1}{2}{3}{4}{5}{6}{7}
1 2 3
6 5 6
4 4
3 8
4 5 6

4 7 3
7

CS 312 – Greedy Algorithms 32


Kruskal’s Algorithm
Sort edges by weight
1: {1,2} {1}{2}{3}{4}{5}{6}{7}
2: {2,3}
1 2 3: {4,5}
1 2 3 3: {6,7}
6 5 4: {1,4}
4 4 6
4: {2,5}
3 8 4: {4,7}
4 5 6
5: {3,5}
7 6: {3,6}
4 3 6: {2,4}
7 7: {5,7}
8: {5,6}
CS 312 – Greedy Algorithms 33
Kruskal’s Algorithm
Add first edge to X if no cycle created
1: {1,2} {1}{2}{3}{4}{5}{6}{7}
2: {2,3}
3: {4,5}
1 2
1 2 3 3: {6,7}
4: {1,4}
6 5 6
4 4 4: {2,5}
3 8 4: {4,7}
4 5 6 5: {3,5}
6: {3,6}
4 7 3 6: {2,4}
7: {5,7}
7
8: {5,6}
CS 312 – Greedy Algorithms 34
Kruskal’s Algorithm
Merge vertices in added edges
1: {1,2} {1,2}{3}{4}{5}{6}{7}
2: {2,3}
3: {4,5}
1 2 3: {6,7}
1 2 3
4: {1,4}
6 5 6 4: {2,5}
4 4
4: {4,7}
3 8
4 5 6 5: {3,5}
6: {3,6}
4 7 3 6: {2,4}
7: {5,7}
7 8: {5,6}

CS 312 – Greedy Algorithms 35


Kruskal’s Algorithm
Process each edge in order
1: {1,2} {1,2}{3}{4}{5}{6}{7}
2: {2,3} {1,2,3}{4}{5}{6}{7}
3: {4,5}
1 2 3: {6,7}
1 2 3
4: {1,4}
6 5 6
4 4 4: {2,5}
3 8 4: {4,7}
4 5 6 5: {3,5}
6: {3,6}
4 7 3 6: {2,4}
7: {5,7}
7
8: {5,6}

CS 312 – Greedy Algorithms 36


Kruskal’s Algorithm
1: {1,2} {1,2}{3}{4}{5}{6}{7}
2: {2,3} {1,2,3}{4}{5}{6}{7}
3: {4,5} {1,2,3}{4,5}{6}{7}
3: {6,7}
1 2 4: {1,4}
1 2 3
4: {2,5}
6 5 6
4 4 4: {4,7}
3 8 5: {3,5}
4 5 6 6: {3,6}
6: {2,4}
4 7 3 7: {5,7}
8: {5,6}
7

Note that each set is a connected component


CS 312 of G
– Greedy Algorithms 37
Kruskal’s Algorithm
1: {1,2} {1,2}{3}{4}{5}{6}{7}
2: {2,3} {1,2,3}{4}{5}{6}{7}
3: {4,5} {1,2,3}{4,5}{6}{7}
3: {6,7} {1,2,3}{4,5}{6,7}
1 2
1 2 3 4: {1,4}
4: {2,5}
6 5 6
4 4 4: {4,7}
3 8 5: {3,5}
4 5 6 6: {3,6}
6: {2,4}
4 7 3 7: {5,7}
7 8: {5,6}

CS 312 – Greedy Algorithms 38


Kruskal’s Algorithm
1: {1,2} {1,2}{3}{4}{5}{6}{7}
2: {2,3} {1,2,3}{4}{5}{6}{7}
3: {4,5} {1,2,3}{4,5}{6}{7}
1 2 3: {6,7} {1,2,3}{4,5}{6,7}
1 2 3 4: {1,4} {1,2,3,4,5}{6,7}
6 5 4: {2,5}
4 4 6 4: {4,7}
3 8 5: {3,5}
4 5 6
6: {3,6}
7 6: {2,4}
4 3 7: {5,7}
7 8: {5,6}

CS 312 – Greedy Algorithms 39


Kruskal’s Algorithm
Must join separate components
1: {1,2} {1,2}{3}{4}{5}{6}{7}
2: {2,3} {1,2,3}{4}{5}{6}{7}
3: {4,5} {1,2,3}{4,5}{6}{7}
1 2 3: {6,7} {1,2,3}{4,5}{6,7}
1 2 3
4: {1,4} {1,2,3,4,5}{6,7}
6 5 6 4: {2,5} rejected
4 4
3 8 4: {4,7}
4 5 6 5: {3,5}
6: {3,6}
4 7 3 6: {2,4}
7: {5,7}
7
8: {5,6}

CS 312 – Greedy Algorithms 40


Kruskal’s Algorithm
Done when all vertices in one set. Then they are all connected
Exactly |V| - 1 edges
1: {1,2} {1,2}{3}{4}{5}{6}{7}
2: {2,3} {1,2,3}{4}{5}{6}{7}
1 2 3: {4,5} {1,2,3}{4,5}{6}{7}
1 2 3
3: {6,7} {1,2,3}{4,5}{6,7}
6 5 6 4: {1,4} {1,2,3,4,5}{6,7}
4 4
4: {2,5} rejected
3 8 4: {4,7} {1,2,3,4,5,6,7} done
4 5 6
5: {3,5}
7 6: {3,6}
4 3
6: {2,4}
7 7: {5,7}
8: {5,6}
CS 312 – Greedy Algorithms 41
Graph Traversals Algorithms:

1. BFS -Breadth First Search


2. DFS -Depth First Search
Graph Traversals Algorithms:

The Term used to traverse the graph are:


1.Visit the Vertex.
2.Explore the Vertex
BFS -Breadth First Search

In Breadth First Search


1. Visit any Node form Graph.
2. Explore all the vertices of visited Vertex.
3. Visit the Next Vertex.
4. Go to step 2
BFS -Breadth First Search
In Breadth First Search
1. Visit any Node form
A B Graph.
i.e. Select A
G

F I H C

E D
BFS -Breadth First Search
In Breadth First Search
1.Visit any Node form
A B Graph.
i.e. Select A
G
2. Explore all the vertices
F I H C of visited Vertex.
A G B F
E D
BFS -Breadth First Search
In Breadth First Search
1.Visit any Node form Graph.
i.e. Select A
A B
2. Explore all the vertices of
visited Vertex.
G
A G B F
F I H C 3. Visit the Next Vertex.
G
E D
4. Go to step 2
BFS -Breadth First Search
In Breadth First Search
1.Visit any Node form Graph.
i.e. Select A
A B
2. Explore all the vertices of
visited Vertex.
G
A G B F
F I H C 3. Visit the Next Vertex.
G I H B A
E D
4. Go to step 2

Continue until all the graph


vertices are explored.
BFS -Breadth First Search
In Breadth First Search
1.Visit any Node form Graph. A
2. Explore all the vertices of visited
Vertex.
3. Visit the Next Vertex.
4. Go to step 2 G B F
A B

F I H C

E D
BFS -Breadth First Search
In Breadth First Search
1.Visit any Node form Graph. A
2. Explore all the vertices of visited
Vertex.
3. Visit the Next Vertex.
4. Go to step 2 G B F
A B

G H I

F I H C

E D
BFS -Breadth First Search
In Breadth First Search
1.Visit any Node form Graph. A
2. Explore all the vertices of visited
Vertex.
3. Visit the Next Vertex.
4. Go to step 2 G B F
A B

G H I C

F I H C

E D
BFS -Breadth First Search
In Breadth First Search
1.Visit any Node form Graph. A
2. Explore all the vertices of visited
Vertex.
3. Visit the Next Vertex.
4. Go to step 2 G B F
A B

G H I C E

F I H C

E D
BFS -Breadth First Search
In Breadth First Search
1.Visit any Node form Graph. A
2. Explore all the vertices of visited
Vertex.
3. Visit the Next Vertex.
4. Go to step 2 G B F
A B

G H I C E

F I H C
D

E D
BFS -Breadth First Search
In Breadth First Search
1.Visit any Node form Graph. A
2. Explore all the vertices of visited
Vertex.
3. Visit the Next Vertex.
4. Go to step 2 G B F
A B

G H I C E

F I H C
D

E D
BFS -Breadth First Search
In Breadth First Search
1.Visit any Node form Graph. A
2. Explore all the vertices of visited
Vertex.
3. Visit the Next Vertex.
4. Go to step 2 G B F
A B

G H I C E

F I H C
D

E D
BFS -Breadth First Search
In Breadth First Search
1.Visit any Node form Graph. A
2. Explore all the vertices of visited
Vertex.
3. Visit the Next Vertex.
4. Go to step 2 G B F
A B

G H I C E

F I H C
D

E D
BFS -Breadth First Search
In Breadth First Search
1.Visit any Node form Graph. A
2. Explore all the vertices of visited
Vertex.
3. Visit the Next Vertex.
4. Go to step 2 G B F
A B

G H I C E

F I H C
D

E D
DFS -Depth First Search

In Depth First Search


1.
DFS -Depth First Search
A B

F I H C

X Y

Z K
DFS -Depth First Search
A B A

F I H C

X Y

Z K
A

Stack
DFS -Depth First Search
A B A

G G

F I H C

X Y

G
Z K
A

Stack
DFS -Depth First Search
A B A

G G

F I H C
I

X Y
I
G
Z K
A

Stack
DFS -Depth First Search
A B A

G G

F I H C
F I

X Y F
I
G
Z K
A

Stack
DFS -Depth First Search
A B A

G G

F I H C
F I

X Y F
I
G
Z K
A

Stack
DFS -Depth First Search
A B A

G G

F I H C
F I

X Y F
Node ‘F’ do not have I
another linked node, G
Z K
So remove from stack A

Stack
DFS -Depth First Search
A B A

G G

F I H C
F I

X Y
Now check the remaining I
Linked Node of ‘I’ G
Z K
A

Stack
DFS -Depth First Search
A B A

G G

F I H C
F I

X Y H
H
I
G
Z K
A

Stack
DFS -Depth First Search
A B A

G G

F I H C
F I
C

X Y H
H C
I
G
Z K
A

Stack
DFS -Depth First Search
A B A

G G

F I H C
F I
C

X Y H
H C
I
G
Z K
Node ‘C’ do not have A
another linked node,
So remove from stack
Stack
DFS -Depth First Search
A B A

G G

F I H C
F I

X Y H
H C
I
G
Z K
Now check the remaining A
Linked Node of ‘H’
Stack
DFS -Depth First Search
A B A

G G

F I H C
F I
Y

X Y H
H C
Y I
G
Z K
A

Stack
DFS -Depth First Search
A B A

G G

F I H C
F I K
Y

X Y H
H C
Y I
G
Z K
A
K

Stack
DFS -Depth First Search
A B A

G G

F I H C
F I K
Y

X Y H
H C
Y I
G
Z K
A
K
Node ‘K’ not has
another linked node, Stack
So remove from stack
DFS -Depth First Search
A B A

G G

F I H C
F I
Y

X Y H
H C
Y I
G
Z K
A
K
Now check the remaining Stack
Linked Node of ‘Y’
DFS -Depth First Search
A B A

G G

F I H C
F I
Z
Y
X Y H C H
Y I
Z K G
Z K A

Stack
DFS -Depth First Search
A B A

G G

F I H C
X
F I
Z
Y
X Y H C H
Y I
Z K G
Z K A

Stack
X
DFS -Depth First Search
A B A

G G

F I H C
F I
Z
Y
X Y H C H
Y I
Z K G
Z K A

Stack
X
DFS -Depth First Search
A B A

G G

F I H C
F I
Y
X Y H C H
Y I
Z K G
Z K A

Stack
X
DFS -Depth First Search
A B A

G G

F I H C
F I

X Y H C H
Y I
Z K G
Z K A

Stack
X
DFS -Depth First Search
A B A

G G

F I H C
F I

X Y H C
Y I
Z K G
Z K A

Stack
X
DFS -Depth First Search
A B A

G G

F I H C
F I

X Y H C
Y
Z K G
Z K A

Stack
X
DFS -Depth First Search
A B A

G G B

F I H C
F I

X Y H C
Y B
Z K G
Z K A

Stack
X
DFS -Depth First Search
A B A

G G B

F I H C
F I

X Y H C
Y
Z K G
Z K A

Stack
X
DFS -Depth First Search
A B A

G G B

F I H C
F I

X Y H C
Y
Z K
Z K A

Stack
X
DFS -Depth First Search
A B A

G G B

F I H C
F I

X Y H C
Y
Z K
Z K

Stack
X
Activity Selection Problem
Scheduling a resource among several
competing activities
 Suppose we have a set S = {1, 2,…..,} of n
proposed activities that wish to use a
resource.
 The resource can be used by only one activity
at a time.
 Each activity i has a start time si and a finish
time fi, where si <= fi.
 If selected activity i takes place during the
half-open time interval si, fi).
 Activities i and j are compatible if the
intervals si, fi) and sj, fj) do not overlap if si
>= fj or sj >= fi.
Activity Selection Problem

 The activity selection problem is


 Select a maximum size set of mutually
compatible activities.
Example:
( 6,10), (2,13), (8,12), (6,10), (8,11), (5,9),
(0,6), (5,7), (3,8), (3,5), (1,4)
Activity Selection Problem
 How should we design the algorithm for
this problem
 Dynamic / Divide & Conquer
 Or Any other way?
Example: Sorted
(1, 4)
( 6,10), (2,13), (8,12), (6,10), (8,11), (5,9), (3, 5)

(0,6), (5,7), (3,8), (3,5), (1,4) (0, 6)


(5, 7)
(3, 8)
Activity(a) A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 (5, 9)
Start(s) 1 3 0 5 3 5 6 6 8 8 2 (6, 10)
Finish(f) 4 5 6 7 8 9 10 10 11 12 13
(6, 10)
(8, 11)
(8, 12)
(2, 13)
Activity Selection Problem
Scheduling a resource among
several competing activities
Activity(a) A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11
Start(s) 1 3 0 5 3 5 6 6 8 8 2
Finish(f) 4 5 6 7 8 9 10 10 11 12 13

0 1 2 3 4 5 6 7 8 9 10 11 12 13

A11
A6 A7
A5
A4 A10
A3 A9
A2 A8
A1

A1 A4 A9
Activity(a)
Start(s) 1
Activity
A1
3
A2
0 5
Selection
A3
3 5
A4
6
A5
6
Problem
8
A6
8 2
A7 A8 A9 A10 A11

Finish(f) 4 5 6 7 Algorithm
8 9 10 10 11 12 13

 Assume that the input activities are in order by increasing


finishing time: f1<=f2<=f3…<=fn
Greedy_Activity_Selector(s, f) //s and f are arrays
n = length[s];
A = {A1}
j = 1 //Activity 1 is selected
for i = 2 to n
if si >= fj
A = A U { i } //Activity I is added to a set of //compatible activities.
j = i;
return A
Apply Prim’s Traverse the Graph
Algorithm using DFS

A B
A 2 B
3 6 G
7 4
G
1 3 F I H C
F 5 I 4 H 2 C
2 8
6 1 2 X Y
E D

Z K

You might also like