SlideShare ist ein Scribd-Unternehmen logo
1 von 50
Downloaden Sie, um offline zu lesen
G h R i dGraph Representation and
Traversals
Deepak John
Department Of Computer Applications, SJCET-Pala
Graph terminology - overviewp gy
 A graph consists of
t f ti V { }◦ set of vertices V = {v1, v2, ….. vn}
◦ set of edges that connect the vertices E ={e1, e2, …. em}
 Two vertices in a graph are adjacent if there is an edgeg p j g
connecting the vertices.
 Two vertices are on a path if there is a sequences of vertices
beginning with the first one and ending with the second onebeginning with the first one and ending with the second one
 Graphs with ordered edges are directed. For directed graphs,
vertices have in and out degrees.
W i h d G h h l i d i h d Weighted Graphs have values associated with edges.
Deepak John,Department Of IT,CE Poonjar
Graph representation – undirectedp p
h Adj li Adj igraph Adjacency list Adjacency matrix
Graph representation – directed
Deepak John,Department Of IT,CE Poonjar
graph Adjacency list Adjacency matrix
Adjacency Lists RepresentationAdjacency Lists Representation
 A graph of n nodes is represented by a one-dimensional array L of
linked lists, where,
 L[i] is the linked list containing all the nodes adjacent from node
i.
 The nodes in the list L[i] are in no particular order
Deepak John,Department Of IT,CE Poonjar
Pros and Cons of Adjacency Matrices
 P Pros:
 Simple to implement
 Easy and fast to tell if a pair (i,j) is an edge: simply check ify p ( ,j) g p y
A[i][j] is 1 or 0
 Cons:
 No matter how few edges the graph has the matrix takes O(n2) No matter how few edges the graph has, the matrix takes O(n2)
in memory
Pros and Cons of Adjacency Lists
 Pros:
 Saves on space (memory): the representation takes as many
memory words as there are nodes and edge.memory words as there are nodes and edge.
 Cons:
 It can take up to O(n) time to determine if a pair of nodes (i,j) is
d ld h t h th li k d li t L[i] hi h
Deepak John,Department Of IT,CE Poonjar
an edge: one would have to search the linked list L[i], which
takes time proportional to the length of L[i].
Graph Traversal TechniquesGraph Traversal Techniques
 There are two standard graph traversal techniques:
 Depth First Search (DFS) Depth-First Search (DFS)
 Breadth-First Search (BFS)
 In both DFS and BFS, the nodes of the undirected graph are, g p
visited in a systematic manner so that every node is visited
exactly one.
 Both BFS and DFS give rise to a tree: Both BFS and DFS give rise to a tree:
 When a node x is visited, it is labeled as visited, and it is added
to the tree
If h l d f d i i d h If the traversal got to node x from node y, y is viewed as the
parent of x, and x a child of y
Deepak John,Department Of IT,CE Poonjar
Depth-First SearchDepth-First Search
 DFS follows the following rules:
S l t i it d d i it it d t t th t1. Select an unvisited node x, visit it, and treat as the current
node
2. Find an unvisited neighbor of the current node, visit it, andg
make it the new current node;
3. If the current node has no unvisited neighbors, backtrack to
the its parent and make that parent the new current node;the its parent, and make that parent the new current node;
4. Repeat steps 3 and 4 until no more nodes can be visited.
5. If there are still unvisited nodes, repeat from step 1.
Deepak John,Department Of IT,CE Poonjar
• It searches ‘deeper’ the graph when possible.
• Starts at the selected node and explores as far as possible alongStarts at the selected node and explores as far as possible along
each branch before backtracking.
• Vertices go through white, gray and black stages of color.
Whit i iti ll– White – initially
– Gray – when discovered first
– Black – when finished i.e. the adjacency list of the vertex isBlack when finished i.e. the adjacency list of the vertex is
completely examined.
• Also records timestamps for each vertex
d[ ] h th t i fi t di d– d[v]when the vertex is first discovered
– f[v] when the vertex is finished
Deepak John,Department Of IT,CE Poonjar
 Depth-first search: Strategy (for digraph)
 choose a starting vertex, distance d = 0
 vertices are visited in order of increasing distance from the
starting vertex,
 examine One edges leading from vertices (at distance d) to examine One edges leading from vertices (at distance d) to
adjacent vertices (at distance d+1)
 then, examine One edges leading from vertices at distance d+1 to
distance d+2, and so on,
 until no new vertex is discovered, or dead end
 then backtrack one distance back up and try other edges and so then, backtrack one distance back up, and try other edges, and so
on
 until finally backtrack to starting vertex, with no more new vertex
Deepak John,Department Of IT,CE Poonjar
to be discovered.
DFS(G)
1 for each vertex u ∈ V [G]
2 d l [ ] WHITE // l ll ti hit t th i t NIL2 do color[u] ← WHITE // color all vertices white, set their parents NIL
3 π[u] ← NIL
4 time ← 0 // zero out time
5 for each vertex u ∈ V [G] // call only for unexplored vertices5 for each vertex u ∈ V [G] // call only for unexplored vertices
6 do if color[u] = WHITE // this may result in multiple sources
7 then DFS-VISIT(u)
DFS-VISIT(u)
1 color[u] ← GRAY ▹White vertex u has just been discovered.
2 time ← time +12 time ← time +1
3 d[u] time // record the discovery time
4 for each v ∈ Adj[u] ▹Explore edge(u, v).
5 do if color[v] = WHITE5 do if color[v] WHITE
6 then π[v] ← u // set the parent value
7 DFS-VISIT(v) // recursive call
8 color[u] BLACK ▹ Blacken u; it is finished
Deepak John,Department Of IT,CE Poonjar
8 color[u] BLACK Blacken u; it is finished.
9 f [u] ▹ time ← time +1
 forward edges- which point from a node of the tree to one of its
descendants
 back edges-which point from a node to one of its ancestors
 cross edges, is any other edge in graph G. It connects vertices in
two different DFS-tree or two vertices in the same DFS-tree neither
of which is the ancestor of the other.
 tree edges edges which belong to the spanning tree itself are tree edges, edges which belong to the spanning tree itself, are
classified separately from forward edges
Deepak John,Department Of IT,CE Poonjar
Deepak John,Department Of IT,CE Poonjar
Depth first search - analysisDepth first search analysis
 Lines 1-3, initialization take time Θ(V).
 Lines 5-7 take time Θ(V), excluding the time to call the DFS-
VISITVISIT.
 DFS-VISIT is called only once for each node (since it’s called
only for white nodes and the first step in it is to paint the node
)gray).
 Loop on line 4-7 is executed |Adj(v)| times. Since, ∑vєV |Adj(v)| =
Ө (E), the total cost of
DFS-VISIT it θ(E)
Th t t l t f DFS i θ(V+E)The total cost of DFS is θ(V+E)
Deepak John,Department Of IT,CE Poonjar
Breadth-First SearchBreadth First Search
 BFS follows the following rules:
1 Select an unvisited node x visit it have it be the root in a BFS1. Select an unvisited node x, visit it, have it be the root in a BFS
tree being formed. Its level is called the current level.
2. From each node z in the current level, in the order in which
the level nodes were visited, visit all the unvisited neighbors
of z. The newly visited nodes from this level form a new level
that becomes the next current level.
3. Repeat step 2 until no more nodes can be visited.
4. If there are still unvisited nodes, repeat from Step 1.
Deepak John,Department Of IT,CE Poonjar
Breadth first search conceptsBreadth first search - concepts
• To keep track of progress, it colors each vertex - white, gray or
blackblack.
• All vertices start white.
• A vertex discovered first time during the search becomes
hinonwhite.
• All vertices adjacent to black ones are discovered. Whereas, gray
ones may have some white adjacent vertices.y j
• Gray represent the frontier between discovered and undiscovered
vertices.
Deepak John,Department Of IT,CE Poonjar
 Breadth-first search: Strategy (for digraph)
 choose a starting vertex, distance d = 0g ,
 vertices are visited in order of increasing distance from the
starting vertex,
 examine all edges leading from vertices (at distance d) to examine all edges leading from vertices (at distance d) to
adjacent vertices (at distance d+1)
 then, examine all edges leading from vertices at distance d+1
t di t d+2 dto distance d+2, and so on,
 until no new vertex is discovered
 The predecessor of u is stored in the variable π[u]. The predecessor of u is stored in the variable π[u].
Deepak John,Department Of IT,CE Poonjar
BFS - algorithm
BFS(G, s) // G is the graph and s is the starting node
1 for each vertex u ∈ V [G] - {s}
2 do color[u] ← WHITE // color of vertex u[ ]
3 d[u] ← ∞ // distance from source s to vertex u
4 π[u] ← NIL // predecessor of u
5 color[s] ← GRAY
6 d[ ] 06 d[s] ← 0
7 π[s] ← NIL
8 Q ← Ø // Q is a FIFO - queue
9 ENQUEUE(Q, s)Q (Q, )
10 while Q ≠ Ø // iterates as long as there are gray vertices. Lines 10-18
11 do u ← DEQUEUE(Q)
12 for each v ∈ Adj[u]
13 d if l [ ] WHITE // di h di d dj i13 do if color[v] = WHITE // discover the undiscovered adjacent vertices
14 then color[v] ← GRAY // enqueued whenever painted gray
15 d[v] ← d[u] + 1
16 π[v] ← u
Deepak John,Department Of IT,CE Poonjar
16 π[v] u
17 ENQUEUE(Q, v)
18 color[u] ← BLACK // painted black whenever dequeued
Deepak John,Department Of IT,CE Poonjar
Breadth first search - analysis
•The while-loop in breadth-first search is executed at most |V|
times. The reason is that every vertex enqueued at most once. So,y q ,
we have O(V).
•The for-loop inside the while-loop is executed at most |E| times
if G is a directed graph or 2|E| times if G is undirected Theif G is a directed graph or 2|E| times if G is undirected. The
reason is that every vertex dequeued at most once and we
examine (u, v) only when u is dequeued. Therefore, every edge
i d if di d i if di dexamined at most once if directed, at most twice if undirected.
So, we have O(E).
•Therefore, the total running time for breadth-first search, g
traversal is O(V + E).
Deepak John,Department Of IT,CE Poonjar
STRONGLY CONNECTED COMPONENTS OF A
DIRECTED GRAPHDIRECTED GRAPH
 A directed graph is called strongly connected if there is a path
from each vertex in the graph to every other vertex.
 The strongly connected components of a directed graph G are
its maximal strongly connected sub graphs.
Deepak John,Department Of IT,CE Poonjar
Graph with strongly connected components marked
PropertiesProperties
 Reflexive property: For all a, a # a. Any vertex is strongly
connected to itself, by definition., y
 Symmetric property: If a # b, then b # a. For strong connectivity,
this follows from the symmetry of the definition. The same two
th ( f t b d th f b t ) th t h th tpaths (one from a to b and another from b to a) that show that a ~
b, looked at in the other order (one from b to a and another from a
to b) show that b ~ a.
 Transitive property: If a # b and b # c, then a # c. Let's expand
this out for strong connectivity: if a ~ b and b ~ c, we have four
paths: a b b a b c and c b Concatenating them in pairs a b cpaths: a-b, b-a, b-c, and c-b. Concatenating them in pairs a-b-c
and c-b-a produces two paths connecting a-c and c-a, so a ~ c,
showing that the transitive property holds for strong connectivity.
Deepak John,Department Of IT,CE Poonjar
Algorithm to Find Strongly Connected ComponentAlgorithm to Find Strongly Connected Component
 Strategy:
Phase 1:
 A standard depth-first search on G is performed, and the
vertices are put in a stack at their finishing times
Ph 2Phase 2:
 A depth-first search is performed on GT, the transpose graph.
 To start a search vertices are popped off the stack To start a search, vertices are popped off the stack.
 A strongly connected component in the graph is identified by
the name of its starting vertex (call leader).
Deepak John,Department Of IT,CE Poonjar
Deepak John,Department Of IT,CE Poonjar
Deepak John,Department Of IT,CE Poonjar
Deepak John,Department Of IT,CE Poonjar
Bi-connected components of an Undirected graph
 Biconnected graph:
 A connected undirected graph G is said to
b bi t d if it i t dbe biconnected if it remains connected
after removal of any one vertex and the
edges that are incident upon that vertex.
 Bi t d t Biconnected component:
 A biconnected component of a undirected
graph is a maximal biconnected subgraph,
that is a biconnected s bgraph notthat is, a biconnected subgraph not
contained in any larger biconnected
subgraph.
Artic lation point:Artic lation points are theArticulation point:Articulation points are the
points where the graph can be broken down
into its biconnected components
 C is an articulation point C is an articulation point
Deepak John,Department Of IT,CE Poonjar
Discovery of Biconnected Components via
Articulation PointsArticulation Points
If we can find articulation points then can compute biconnected
components.
Idea:
• During DFS, use stack to store visited edges.
E h ti l t th DFS f t hild f ti l ti• Each time we complete the DFS of a tree child of an articulation
point, pop all stacked edges currently in stack
• These popped off edges form a biconnected component.p pp g p
Deepak John,Department Of IT,CE Poonjar
Bi-connected components,
 Undirected graph
Deepak John,Department Of IT,CE Poonjar
biconnected components
Deepak John,Department Of IT,CE Poonjar
Deepak John,Department Of IT,CE Poonjar
Deepak John,Department Of IT,CE Poonjar
WHAT IS BINARY RELATION
 A binary relation R from the set S to the set T is a subset
of S×T, R S×T. If S = T, we say that the relation is a binary
l i Srelation on S.
 P ti f Bi R l ti Properties of Binary Relation
Let R be a binary relation on S.Then R is
1 Reflexive1. Reflexive
2. Symmetric
3 Anti-symmetric3. Anti-symmetric
4. Transitive
Deepak John,Department Of IT,CE Poonjar
Transitive closure of a graphTransitive closure of a graph
The problem: Given a directed graph, G = (V, E), find all of the
i h bl f i i Vvertices reachable from a given starting vertex v ϵV.
Transitive closure (definition): Let G = (V E) be a graph where x RTransitive closure (definition): Let G = (V, E) be a graph, where x R
y, y R z (x, y, z ϵ V). Then we can add a new edge x R z. A graph
containing all of the edges of this nature is called the transitive
closure of the original graph.
The best way to represent the transitive closure graph (TCG) is byThe best way to represent the transitive closure graph (TCG) is by
means of an adjacency matrix.
Deepak John,Department Of IT,CE Poonjar
 Consider the following adjacency matrix on the left representing a
directed graph, the transitive closure is given on the right illustratingg p , g g g
which vertices can reach other vertices
•there is an edge from a to b and e b d b d•there is an edge from a to b and e.
•b can reach d
•d can reach c
h ll i
a b c d e a b c d e
a 0 1 0 0 1 1 1 1 1 1
b 0 0 0 1 0 0 1 1 1 0
•a can reach all vertices.
•But b cannot reach a
c 0 1 0 0 0 0 1 1 1 0
d 0 0 1 0 0 0 1 1 1 0
e 0 0 0 1 0 0 1 1 1 1e 0 0 0 1 0 0 1 1 1 1
Deepak John,Department Of IT,CE Poonjar
Strategy for Transitive ClosureStrategy for Transitive Closure
 We noted earlier that if there is an path from a to b and from b to c,
then there is a path from a to c
 Our strategy for deriving a transitive closure matrix will be based on
this simple idea
 Start with a, compare it against each other vertex and see if there is, p g
an edge
 if so, the corresponding matrix value is true
 if not see if there is already a path known from some vertex c to if not, see if there is already a path known from some vertex c to
b and an edge from a to b, if so, then we know that there is a path
from a to b
Thi ill i th f 3 t d f l f th t ti This will require the use of 3 nested for-loops, one for the starting
vertex of a path, one for the destination vertex of a path, and one to
see if a path already exists from start to this point and from this
i d i i
Deepak John,Department Of IT,CE Poonjar
point to destination
It implies the following rules for generating R(k) from R(k-1):
RR((kk))[[i ji j]] == RR((kk--11))[[i ji j]] oror ((RR((kk--11))[[i ki k]] andand RR((kk--11))[[k jk j])])RR(( ))[[i,ji,j]] RR(( ))[[i,ji,j]] oror ((RR(( ))[[i,ki,k]] andand RR(( ))[[k,jk,j])])
 Rule 1 If an element in row i and column j is 1 in R(k-1),it remains 1
in R(k)
 Rule 2 If an element in row i and column j is 0 in R(k-1), it has to be
changed to 1 in R(k) if and only if the element in its row i and columnchanged to 1 in R(k) if and only if the element in its row i and column
k and the element in its column j and row k are both 1’s in R(k-1)
 Constructs transitive closure T as the last matrix in the sequence of
n-by-n matrices R(0), … , R(k), … , R(n)
 Note that R(0) = A (adjacency matrix), R(n) = T (transitive closure)
Deepak John,Department Of IT,CE Poonjar
Warshall’s Algorithmg
Deepak John,Department Of IT,CE Poonjar
 It should be obvious that the complexity is O(n3) because of the 3
nested for-loops.
 The result is an NxN matrix where entry R[i, j] is true if there is a
path from vertex i to vertex j.
 The algorithm will work on either undirected or directed The algorithm will work on either undirected or directed
graphs
Warshall’s Algorithm (example)
3
1
3
1
3
1
42
0 0 1 0
42 42
0 0 1 0
1 0 0 1
0 0 0 0R(0) =
0 0 1 0
1 0 1 1
0 0 0 0R(1) =
0 0 1 0
1 1 1 1
0 0 0 0R(2) =
0 1 0 0 0 1 0 0
0 0 0 0
1 1 1 1
33
4
1
3
1
0 0 1 0
1 1 1 1
42
0 0 1 0
1 1 1 1
42
Deepak John,Department Of IT,CE Poonjar
1 1 1 1
0 0 0 0
1 1 1 1
R(3) =
1 1 1 1
0 0 0 0
1 1 1 1
R(4) =
All-Pairs Shortest PathsAll Pairs Shortest Paths
 Given a weighted graph G(V,E,w), the all-pairs shortest paths
problem is to find the shortest paths between all pairs of vertices
vi, vj ∈ V.
 A number of algorithms are known for solving this problem A number of algorithms are known for solving this problem.
FLOYD’S ALGORITHM: ALL PAIRS SHORTEST PATHSFLOYD’S ALGORITHM: ALL PAIRS SHORTEST PATHS
Problem: In a weighted (di)graph, find shortest paths between every
pair of vertices
Same idea: construct solution through series of matrices D(0), …,D (n)
Deepak John,Department Of IT,CE Poonjar
Time efficiency: Θ(n3)
Space efficiency: Matrices can be written over their predecessors
Deepak John,Department Of IT,CE Poonjar
Space efficiency: Matrices can be written over their predecessors
Floyd’s Algorithm (example)
0 ∞ 3 ∞
2 0
0 ∞ 3 ∞
2 0 5
21 2
2 0 ∞ ∞
∞ 7 0 1
6 ∞ ∞ 0
D(0) =
2 0 5 ∞
∞ 7 0 1
6 ∞ 9 0
D(1) =
3
1
3 6 7
4
0 3 0 10 3 4 0 10 3 4
1
0 ∞ 3 ∞
2 0 5 ∞
9 7 0 1D(2) =
0 10 3 4
2 0 5 6
9 7 0 1D(3) =
0 10 3 4
2 0 5 6
7 7 0 1D(4) =
6 ∞ 9 0 6 16 9 0 6 16 9 0
Deepak John, Department Of IT,CE Poonjar
Dynamic Programmingy g g
 Dynamic Programming is an algorithm design technique for
optimization problems: often minimizing or maximizing.p p g g
 Like divide and conquer, DP solves problems by combining
solutions to sub problems.
U lik di id d b bl t i d d t Unlike divide and conquer, sub problems are not independent.
 Sub problems may share subsubproblems,
 However, solution to one sub problem may not affect the solutions to other sub
bl f h blproblems of the same problem.
 DP reduces computation by
 Solving sub problems in a bottom-up fashion.g p p
 Storing solution to a sub problem the first time it is solved.
 Looking up the solution when sub problem is encountered again.
 Key: determine structure of optimal solutions
Deepak John,Department Of IT,CE Poonjar
 Key: determine structure of optimal solutions
Steps in Dynamic Programming
1 Characterize structure of an optimal solution1. Characterize structure of an optimal solution.
2. Define value of optimal solution recursively.
3. Compute optimal solution valuesp p
4. Construct an optimal solution from computed values.
Elements of Dynamic Programming
 Optimal substructure
 Overlapping subproblems
Deepak John,Department Of IT,CE Poonjar
Optimal Binary Search TreesOptimal Binary Search Trees
 OBST is one special kind of advanced tree.
 It focus on how to reduce the cost of the search of the BST It focus on how to reduce the cost of the search of the BST.
 A good example of a dynamic algorithm
• Solves all the small problems
ild l i l bl f h• Builds solutions to larger problems from them
• Requires space to store small problem results
Deepak John,Department Of IT,CE Poonjar
 Problem
 Given sequence K = k1 < k2 <··· < kn of n sorted keys, with aq 1 2 n y ,
search probability pi for each key ki.
 Want to build a binary search tree (BST)
with minimum expected search costwith minimum expected search cost.
 Actual cost = number of items examined.
 For key ki, cost = depthT(ki)+1, where For key ki, cost depthT(ki) 1, where
depthT(ki) = depth of ki in BST T .
TE ]incostsearch[
 
n
i
iiT pk
TE
1
)(depth1
]incostsearch[
i 1
 Consider 5 keys with these search probabilities:
p1 = 0.25, p2 = 0.2, p3 = 0.05, p4 = 0.2, p5 = 0.3.
k2 i depthT(ki) depthT(ki)·pi
1 1 0 25
k1 k4
1 1 0.25
2 0 0
3 2 0.1
4 1 0 2
k3 k5
4 1 0.2
5 2 0.6
1.15
k3 k5
Therefore, [search cost] = 2.15.
Deepak John,Department Of IT,CE Poonjar
 p1 = 0.25, p2 = 0.2, p3 = 0.05, p4 = 0.2, p5 = 0.3.
i depthT(ki) depthT(ki)·pi
1 1 0.25
2 0 0
k2
2 0 0
3 3 0.15
4 2 0.4
5 1 0 3
k1 k5
5 1 0.3
1.10
k4
Therefore, E[search cost] = 2.10.
k3 This tree turns out to be optimal for this set of keys
Deepak John,Department Of IT,CE Poonjar
3 This tree turns out to be optimal for this set of keys.
Optimal Substructure
 Any sub tree of a BST contains keys in a contiguous range ki,
..., kj Tj T
T’
 If T is an optimal BST and T contains sub tree T’ with keys ki,
... ,kj , then T’must be an optimal BST for keys ki, ..., kj.
Deepak John,Department Of IT,CE Poonjar
, j , p y i, , j
1
Pseudo-code
OPTIMAL-BST(p, q, n)OPTIMAL-BST(p, q, n)(p q )
1. for i ← 1 to n + 1
2. do e[i, i 1] ← 0
3. w[i, i 1] ← 0
(p q )
1. for i ← 1 to n + 1
2. do e[i, i 1] ← 0
3. w[i, i 1] ← 0
4. for l ← 1 to n
5. do for i ← 1 to nl + 1
6. do j ←i + l1
4. for l ← 1 to n
5. do for i ← 1 to nl + 1
6. do j ←i + l1
7. e[i, j ]←∞
8. w[i, j ] ← w[i, j1] + pj
9. for r ←i to j
7. e[i, j ]←∞
8. w[i, j ] ← w[i, j1] + pj
9. for r ←i to j
10. do t ← e[i, r1] + e[r + 1, j ] + w[i, j ]
11. if t < e[i, j ]
12. then e[i, j ] ← t
10. do t ← e[i, r1] + e[r + 1, j ] + w[i, j ]
11. if t < e[i, j ]
12. then e[i, j ] ← t
13. root[i, j ] ←r
14. return e and root
13. root[i, j ] ←r
14. return e and root

Weitere ähnliche Inhalte

Was ist angesagt?

Breadth first search and depth first search
Breadth first search and  depth first searchBreadth first search and  depth first search
Breadth first search and depth first searchHossain Md Shakhawat
 
lecture 17
lecture 17lecture 17
lecture 17sajinsc
 
2.5 bfs & dfs 02
2.5 bfs & dfs 022.5 bfs & dfs 02
2.5 bfs & dfs 02Krish_ver2
 
Depth First Search ( DFS )
Depth First Search ( DFS )Depth First Search ( DFS )
Depth First Search ( DFS )Sazzad Hossain
 
Depth first search and breadth first searching
Depth first search and breadth first searchingDepth first search and breadth first searching
Depth first search and breadth first searchingKawsar Hamid Sumon
 
Breadth First Search (BFS)
Breadth First Search (BFS)Breadth First Search (BFS)
Breadth First Search (BFS)Dhrumil Panchal
 
Graph traversal-BFS & DFS
Graph traversal-BFS & DFSGraph traversal-BFS & DFS
Graph traversal-BFS & DFSRajandeep Gill
 
Graph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search TraversalGraph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search TraversalAmrinder Arora
 
Breadth first search signed
Breadth first search signedBreadth first search signed
Breadth first search signedAfshanKhan51
 
Uniformed tree searching
Uniformed tree searching Uniformed tree searching
Uniformed tree searching Ayaelshiwi
 
AI - Backtracking vs Depth-First Search (DFS)
AI - Backtracking vs Depth-First Search (DFS)AI - Backtracking vs Depth-First Search (DFS)
AI - Backtracking vs Depth-First Search (DFS)Johnnatan Messias
 
Breadth first search (Bfs)
Breadth first search (Bfs)Breadth first search (Bfs)
Breadth first search (Bfs)Ishucs
 
Lecture 12 Heuristic Searches
Lecture 12 Heuristic SearchesLecture 12 Heuristic Searches
Lecture 12 Heuristic SearchesHema Kashyap
 

Was ist angesagt? (20)

Breadth first search and depth first search
Breadth first search and  depth first searchBreadth first search and  depth first search
Breadth first search and depth first search
 
lecture 17
lecture 17lecture 17
lecture 17
 
Iterative deepening search
Iterative deepening searchIterative deepening search
Iterative deepening search
 
2.5 bfs & dfs 02
2.5 bfs & dfs 022.5 bfs & dfs 02
2.5 bfs & dfs 02
 
BFS
BFSBFS
BFS
 
Depth First Search ( DFS )
Depth First Search ( DFS )Depth First Search ( DFS )
Depth First Search ( DFS )
 
Graphs bfs dfs
Graphs bfs dfsGraphs bfs dfs
Graphs bfs dfs
 
Depth first search and breadth first searching
Depth first search and breadth first searchingDepth first search and breadth first searching
Depth first search and breadth first searching
 
130210107039 2130702
130210107039 2130702130210107039 2130702
130210107039 2130702
 
2.5 graph dfs
2.5 graph dfs2.5 graph dfs
2.5 graph dfs
 
Breadth First Search (BFS)
Breadth First Search (BFS)Breadth First Search (BFS)
Breadth First Search (BFS)
 
Graph traversal-BFS & DFS
Graph traversal-BFS & DFSGraph traversal-BFS & DFS
Graph traversal-BFS & DFS
 
Graph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search TraversalGraph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search Traversal
 
Breadth first search signed
Breadth first search signedBreadth first search signed
Breadth first search signed
 
Uniformed tree searching
Uniformed tree searching Uniformed tree searching
Uniformed tree searching
 
Breadth first search
Breadth first searchBreadth first search
Breadth first search
 
AI - Backtracking vs Depth-First Search (DFS)
AI - Backtracking vs Depth-First Search (DFS)AI - Backtracking vs Depth-First Search (DFS)
AI - Backtracking vs Depth-First Search (DFS)
 
Breadth first search (Bfs)
Breadth first search (Bfs)Breadth first search (Bfs)
Breadth first search (Bfs)
 
Graph
GraphGraph
Graph
 
Lecture 12 Heuristic Searches
Lecture 12 Heuristic SearchesLecture 12 Heuristic Searches
Lecture 12 Heuristic Searches
 

Ähnlich wie Analysis and design of algorithms part 3

Depth first search [dfs]
Depth first search [dfs]Depth first search [dfs]
Depth first search [dfs]DEEPIKA T
 
B.tech admission in india
B.tech admission in indiaB.tech admission in india
B.tech admission in indiaEdhole.com
 
Analysis of Pathfinding Algorithms
Analysis of Pathfinding AlgorithmsAnalysis of Pathfinding Algorithms
Analysis of Pathfinding AlgorithmsSigSegVSquad
 
DFS ppt.pdf
DFS ppt.pdfDFS ppt.pdf
DFS ppt.pdfRajkk5
 
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docxgraphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docxwhittemorelucilla
 
Graph Representation, DFS and BFS Presentation.pptx
Graph Representation, DFS and BFS Presentation.pptxGraph Representation, DFS and BFS Presentation.pptx
Graph Representation, DFS and BFS Presentation.pptxbashirabdullah789
 
Graph Traversal Algorithm
Graph Traversal AlgorithmGraph Traversal Algorithm
Graph Traversal Algorithmjyothimonc
 
Graph Traversals
Graph TraversalsGraph Traversals
Graph TraversalsMaryJacob24
 
Riya Bepari_34700122020_Artificial Intelligence_PEC-IT501B.pptx
Riya Bepari_34700122020_Artificial Intelligence_PEC-IT501B.pptxRiya Bepari_34700122020_Artificial Intelligence_PEC-IT501B.pptx
Riya Bepari_34700122020_Artificial Intelligence_PEC-IT501B.pptxRIYABEPARI
 
Depth first traversal(data structure algorithms)
Depth first traversal(data structure algorithms)Depth first traversal(data structure algorithms)
Depth first traversal(data structure algorithms)bhuvaneshwariA5
 

Ähnlich wie Analysis and design of algorithms part 3 (20)

Chapter 23 aoa
Chapter 23 aoaChapter 23 aoa
Chapter 23 aoa
 
Depth first search [dfs]
Depth first search [dfs]Depth first search [dfs]
Depth first search [dfs]
 
LEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdfLEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdf
 
kumattt).pptx
kumattt).pptxkumattt).pptx
kumattt).pptx
 
B.tech admission in india
B.tech admission in indiaB.tech admission in india
B.tech admission in india
 
Analysis of Pathfinding Algorithms
Analysis of Pathfinding AlgorithmsAnalysis of Pathfinding Algorithms
Analysis of Pathfinding Algorithms
 
DFS ppt.pdf
DFS ppt.pdfDFS ppt.pdf
DFS ppt.pdf
 
19-graph1 (1).ppt
19-graph1 (1).ppt19-graph1 (1).ppt
19-graph1 (1).ppt
 
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docxgraphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
 
Graph Representation, DFS and BFS Presentation.pptx
Graph Representation, DFS and BFS Presentation.pptxGraph Representation, DFS and BFS Presentation.pptx
Graph Representation, DFS and BFS Presentation.pptx
 
Graph Traversal Algorithm
Graph Traversal AlgorithmGraph Traversal Algorithm
Graph Traversal Algorithm
 
U1 L5 DAA.pdf
U1 L5 DAA.pdfU1 L5 DAA.pdf
U1 L5 DAA.pdf
 
Graph Traversals
Graph TraversalsGraph Traversals
Graph Traversals
 
Graph Theory
Graph TheoryGraph Theory
Graph Theory
 
Graphs
GraphsGraphs
Graphs
 
Riya Bepari_34700122020_Artificial Intelligence_PEC-IT501B.pptx
Riya Bepari_34700122020_Artificial Intelligence_PEC-IT501B.pptxRiya Bepari_34700122020_Artificial Intelligence_PEC-IT501B.pptx
Riya Bepari_34700122020_Artificial Intelligence_PEC-IT501B.pptx
 
Graps 2
Graps 2Graps 2
Graps 2
 
Graph
GraphGraph
Graph
 
Depth first traversal(data structure algorithms)
Depth first traversal(data structure algorithms)Depth first traversal(data structure algorithms)
Depth first traversal(data structure algorithms)
 
ALG5.1.ppt
ALG5.1.pptALG5.1.ppt
ALG5.1.ppt
 

Mehr von Deepak John

Network concepts and wi fi
Network concepts and wi fiNetwork concepts and wi fi
Network concepts and wi fiDeepak John
 
Web browser week5 presentation
Web browser week5 presentationWeb browser week5 presentation
Web browser week5 presentationDeepak John
 
Information management
Information managementInformation management
Information managementDeepak John
 
It security,malware,phishing,information theft
It security,malware,phishing,information theftIt security,malware,phishing,information theft
It security,malware,phishing,information theftDeepak John
 
Email,contacts and calendar
Email,contacts and calendarEmail,contacts and calendar
Email,contacts and calendarDeepak John
 
Module 2 instruction set
Module 2 instruction set Module 2 instruction set
Module 2 instruction set Deepak John
 
introduction to computers
 introduction to computers introduction to computers
introduction to computersDeepak John
 
Registers and counters
Registers and counters Registers and counters
Registers and counters Deepak John
 
Computer security module 4
Computer security module 4Computer security module 4
Computer security module 4Deepak John
 
Module 4 network and computer security
Module  4 network and computer securityModule  4 network and computer security
Module 4 network and computer securityDeepak John
 
Network and computer security-
Network and computer security-Network and computer security-
Network and computer security-Deepak John
 
Computer security module 3
Computer security module 3Computer security module 3
Computer security module 3Deepak John
 
Module 4 registers and counters
Module 4 registers and counters Module 4 registers and counters
Module 4 registers and counters Deepak John
 
Module 2 network and computer security
Module 2 network and computer securityModule 2 network and computer security
Module 2 network and computer securityDeepak John
 
Computer security module 2
Computer security module 2Computer security module 2
Computer security module 2Deepak John
 
Computer security module 1
Computer security module 1Computer security module 1
Computer security module 1Deepak John
 
Network and Computer security
Network and Computer securityNetwork and Computer security
Network and Computer securityDeepak John
 
Combinational and sequential logic
Combinational and sequential logicCombinational and sequential logic
Combinational and sequential logicDeepak John
 
Module 2 logic gates
Module 2  logic gatesModule 2  logic gates
Module 2 logic gatesDeepak John
 

Mehr von Deepak John (20)

Network concepts and wi fi
Network concepts and wi fiNetwork concepts and wi fi
Network concepts and wi fi
 
Web browser week5 presentation
Web browser week5 presentationWeb browser week5 presentation
Web browser week5 presentation
 
Information management
Information managementInformation management
Information management
 
It security,malware,phishing,information theft
It security,malware,phishing,information theftIt security,malware,phishing,information theft
It security,malware,phishing,information theft
 
Email,contacts and calendar
Email,contacts and calendarEmail,contacts and calendar
Email,contacts and calendar
 
Module 1 8086
Module 1 8086Module 1 8086
Module 1 8086
 
Module 2 instruction set
Module 2 instruction set Module 2 instruction set
Module 2 instruction set
 
introduction to computers
 introduction to computers introduction to computers
introduction to computers
 
Registers and counters
Registers and counters Registers and counters
Registers and counters
 
Computer security module 4
Computer security module 4Computer security module 4
Computer security module 4
 
Module 4 network and computer security
Module  4 network and computer securityModule  4 network and computer security
Module 4 network and computer security
 
Network and computer security-
Network and computer security-Network and computer security-
Network and computer security-
 
Computer security module 3
Computer security module 3Computer security module 3
Computer security module 3
 
Module 4 registers and counters
Module 4 registers and counters Module 4 registers and counters
Module 4 registers and counters
 
Module 2 network and computer security
Module 2 network and computer securityModule 2 network and computer security
Module 2 network and computer security
 
Computer security module 2
Computer security module 2Computer security module 2
Computer security module 2
 
Computer security module 1
Computer security module 1Computer security module 1
Computer security module 1
 
Network and Computer security
Network and Computer securityNetwork and Computer security
Network and Computer security
 
Combinational and sequential logic
Combinational and sequential logicCombinational and sequential logic
Combinational and sequential logic
 
Module 2 logic gates
Module 2  logic gatesModule 2  logic gates
Module 2 logic gates
 

Kürzlich hochgeladen

Hotel And Home Service Available Kolkata Call Girls Howrah ✔ 6297143586 ✔Call...
Hotel And Home Service Available Kolkata Call Girls Howrah ✔ 6297143586 ✔Call...Hotel And Home Service Available Kolkata Call Girls Howrah ✔ 6297143586 ✔Call...
Hotel And Home Service Available Kolkata Call Girls Howrah ✔ 6297143586 ✔Call...ritikasharma
 
Independent Diamond Harbour Escorts ✔ 9332606886✔ Full Night With Room Online...
Independent Diamond Harbour Escorts ✔ 9332606886✔ Full Night With Room Online...Independent Diamond Harbour Escorts ✔ 9332606886✔ Full Night With Room Online...
Independent Diamond Harbour Escorts ✔ 9332606886✔ Full Night With Room Online...Riya Pathan
 
Sonagachi ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...
Sonagachi ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...Sonagachi ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...
Sonagachi ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...rahim quresi
 
Tikiapara Call Girls ✔ 8005736733 ✔ Hot Model With Sexy Bhabi Ready For Sex A...
Tikiapara Call Girls ✔ 8005736733 ✔ Hot Model With Sexy Bhabi Ready For Sex A...Tikiapara Call Girls ✔ 8005736733 ✔ Hot Model With Sexy Bhabi Ready For Sex A...
Tikiapara Call Girls ✔ 8005736733 ✔ Hot Model With Sexy Bhabi Ready For Sex A...aamir
 
Science City Kolkata ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sex...
Science City Kolkata ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sex...Science City Kolkata ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sex...
Science City Kolkata ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sex...rahim quresi
 
Call Girls Agency In Goa 💚 9316020077 💚 Call Girl Goa By Russian Call Girl ...
Call Girls  Agency In Goa  💚 9316020077 💚 Call Girl Goa By Russian Call Girl ...Call Girls  Agency In Goa  💚 9316020077 💚 Call Girl Goa By Russian Call Girl ...
Call Girls Agency In Goa 💚 9316020077 💚 Call Girl Goa By Russian Call Girl ...russian goa call girl and escorts service
 
Kanpur call girls 📞 8617697112 At Low Cost Cash Payment Booking
Kanpur call girls 📞 8617697112 At Low Cost Cash Payment BookingKanpur call girls 📞 8617697112 At Low Cost Cash Payment Booking
Kanpur call girls 📞 8617697112 At Low Cost Cash Payment BookingNitya salvi
 
Top Rated Pune Call Girls Dhayari ⟟ 6297143586 ⟟ Call Me For Genuine Sex Ser...
Top Rated  Pune Call Girls Dhayari ⟟ 6297143586 ⟟ Call Me For Genuine Sex Ser...Top Rated  Pune Call Girls Dhayari ⟟ 6297143586 ⟟ Call Me For Genuine Sex Ser...
Top Rated Pune Call Girls Dhayari ⟟ 6297143586 ⟟ Call Me For Genuine Sex Ser...Call Girls in Nagpur High Profile
 
College Call Girls Pune 8617697112 Short 1500 Night 6000 Best call girls Service
College Call Girls Pune 8617697112 Short 1500 Night 6000 Best call girls ServiceCollege Call Girls Pune 8617697112 Short 1500 Night 6000 Best call girls Service
College Call Girls Pune 8617697112 Short 1500 Night 6000 Best call girls ServiceNitya salvi
 
Top Rated Kolkata Call Girls Khardah ⟟ 6297143586 ⟟ Call Me For Genuine Sex S...
Top Rated Kolkata Call Girls Khardah ⟟ 6297143586 ⟟ Call Me For Genuine Sex S...Top Rated Kolkata Call Girls Khardah ⟟ 6297143586 ⟟ Call Me For Genuine Sex S...
Top Rated Kolkata Call Girls Khardah ⟟ 6297143586 ⟟ Call Me For Genuine Sex S...ritikasharma
 
2k Shot Call girls Laxmi Nagar Delhi 9205541914
2k Shot Call girls Laxmi Nagar Delhi 92055419142k Shot Call girls Laxmi Nagar Delhi 9205541914
2k Shot Call girls Laxmi Nagar Delhi 9205541914Delhi Call girls
 
Hotel And Home Service Available Kolkata Call Girls South End Park ✔ 62971435...
Hotel And Home Service Available Kolkata Call Girls South End Park ✔ 62971435...Hotel And Home Service Available Kolkata Call Girls South End Park ✔ 62971435...
Hotel And Home Service Available Kolkata Call Girls South End Park ✔ 62971435...ritikasharma
 
Verified Trusted Call Girls Tambaram Chennai ✔✔7427069034 Independent Chenna...
Verified Trusted Call Girls Tambaram Chennai ✔✔7427069034  Independent Chenna...Verified Trusted Call Girls Tambaram Chennai ✔✔7427069034  Independent Chenna...
Verified Trusted Call Girls Tambaram Chennai ✔✔7427069034 Independent Chenna... Shivani Pandey
 
Dakshineswar Call Girls ✔ 8005736733 ✔ Hot Model With Sexy Bhabi Ready For Se...
Dakshineswar Call Girls ✔ 8005736733 ✔ Hot Model With Sexy Bhabi Ready For Se...Dakshineswar Call Girls ✔ 8005736733 ✔ Hot Model With Sexy Bhabi Ready For Se...
Dakshineswar Call Girls ✔ 8005736733 ✔ Hot Model With Sexy Bhabi Ready For Se...aamir
 
Independent Sonagachi Escorts ✔ 9332606886✔ Full Night With Room Online Booki...
Independent Sonagachi Escorts ✔ 9332606886✔ Full Night With Room Online Booki...Independent Sonagachi Escorts ✔ 9332606886✔ Full Night With Room Online Booki...
Independent Sonagachi Escorts ✔ 9332606886✔ Full Night With Room Online Booki...Riya Pathan
 
Model Call Girls In Pazhavanthangal WhatsApp Booking 7427069034 call girl ser...
Model Call Girls In Pazhavanthangal WhatsApp Booking 7427069034 call girl ser...Model Call Girls In Pazhavanthangal WhatsApp Booking 7427069034 call girl ser...
Model Call Girls In Pazhavanthangal WhatsApp Booking 7427069034 call girl ser... Shivani Pandey
 
VIP Model Call Girls Budhwar Peth ( Pune ) Call ON 8005736733 Starting From 5...
VIP Model Call Girls Budhwar Peth ( Pune ) Call ON 8005736733 Starting From 5...VIP Model Call Girls Budhwar Peth ( Pune ) Call ON 8005736733 Starting From 5...
VIP Model Call Girls Budhwar Peth ( Pune ) Call ON 8005736733 Starting From 5...SUHANI PANDEY
 
𓀤Call On 6297143586 𓀤 Sonagachi Call Girls In All Kolkata 24/7 Provide Call W...
𓀤Call On 6297143586 𓀤 Sonagachi Call Girls In All Kolkata 24/7 Provide Call W...𓀤Call On 6297143586 𓀤 Sonagachi Call Girls In All Kolkata 24/7 Provide Call W...
𓀤Call On 6297143586 𓀤 Sonagachi Call Girls In All Kolkata 24/7 Provide Call W...rahim quresi
 
Zirakpur Call Girls👧 Book Now📱8146719683 📞👉Mohali Call Girl Service No Advanc...
Zirakpur Call Girls👧 Book Now📱8146719683 📞👉Mohali Call Girl Service No Advanc...Zirakpur Call Girls👧 Book Now📱8146719683 📞👉Mohali Call Girl Service No Advanc...
Zirakpur Call Girls👧 Book Now📱8146719683 📞👉Mohali Call Girl Service No Advanc...rajveermohali2022
 

Kürzlich hochgeladen (20)

Hotel And Home Service Available Kolkata Call Girls Howrah ✔ 6297143586 ✔Call...
Hotel And Home Service Available Kolkata Call Girls Howrah ✔ 6297143586 ✔Call...Hotel And Home Service Available Kolkata Call Girls Howrah ✔ 6297143586 ✔Call...
Hotel And Home Service Available Kolkata Call Girls Howrah ✔ 6297143586 ✔Call...
 
CHEAP Call Girls in Malviya Nagar, (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in  Malviya Nagar, (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in  Malviya Nagar, (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Malviya Nagar, (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Independent Diamond Harbour Escorts ✔ 9332606886✔ Full Night With Room Online...
Independent Diamond Harbour Escorts ✔ 9332606886✔ Full Night With Room Online...Independent Diamond Harbour Escorts ✔ 9332606886✔ Full Night With Room Online...
Independent Diamond Harbour Escorts ✔ 9332606886✔ Full Night With Room Online...
 
Sonagachi ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...
Sonagachi ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...Sonagachi ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...
Sonagachi ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...
 
Tikiapara Call Girls ✔ 8005736733 ✔ Hot Model With Sexy Bhabi Ready For Sex A...
Tikiapara Call Girls ✔ 8005736733 ✔ Hot Model With Sexy Bhabi Ready For Sex A...Tikiapara Call Girls ✔ 8005736733 ✔ Hot Model With Sexy Bhabi Ready For Sex A...
Tikiapara Call Girls ✔ 8005736733 ✔ Hot Model With Sexy Bhabi Ready For Sex A...
 
Science City Kolkata ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sex...
Science City Kolkata ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sex...Science City Kolkata ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sex...
Science City Kolkata ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sex...
 
Call Girls Agency In Goa 💚 9316020077 💚 Call Girl Goa By Russian Call Girl ...
Call Girls  Agency In Goa  💚 9316020077 💚 Call Girl Goa By Russian Call Girl ...Call Girls  Agency In Goa  💚 9316020077 💚 Call Girl Goa By Russian Call Girl ...
Call Girls Agency In Goa 💚 9316020077 💚 Call Girl Goa By Russian Call Girl ...
 
Kanpur call girls 📞 8617697112 At Low Cost Cash Payment Booking
Kanpur call girls 📞 8617697112 At Low Cost Cash Payment BookingKanpur call girls 📞 8617697112 At Low Cost Cash Payment Booking
Kanpur call girls 📞 8617697112 At Low Cost Cash Payment Booking
 
Top Rated Pune Call Girls Dhayari ⟟ 6297143586 ⟟ Call Me For Genuine Sex Ser...
Top Rated  Pune Call Girls Dhayari ⟟ 6297143586 ⟟ Call Me For Genuine Sex Ser...Top Rated  Pune Call Girls Dhayari ⟟ 6297143586 ⟟ Call Me For Genuine Sex Ser...
Top Rated Pune Call Girls Dhayari ⟟ 6297143586 ⟟ Call Me For Genuine Sex Ser...
 
College Call Girls Pune 8617697112 Short 1500 Night 6000 Best call girls Service
College Call Girls Pune 8617697112 Short 1500 Night 6000 Best call girls ServiceCollege Call Girls Pune 8617697112 Short 1500 Night 6000 Best call girls Service
College Call Girls Pune 8617697112 Short 1500 Night 6000 Best call girls Service
 
Top Rated Kolkata Call Girls Khardah ⟟ 6297143586 ⟟ Call Me For Genuine Sex S...
Top Rated Kolkata Call Girls Khardah ⟟ 6297143586 ⟟ Call Me For Genuine Sex S...Top Rated Kolkata Call Girls Khardah ⟟ 6297143586 ⟟ Call Me For Genuine Sex S...
Top Rated Kolkata Call Girls Khardah ⟟ 6297143586 ⟟ Call Me For Genuine Sex S...
 
2k Shot Call girls Laxmi Nagar Delhi 9205541914
2k Shot Call girls Laxmi Nagar Delhi 92055419142k Shot Call girls Laxmi Nagar Delhi 9205541914
2k Shot Call girls Laxmi Nagar Delhi 9205541914
 
Hotel And Home Service Available Kolkata Call Girls South End Park ✔ 62971435...
Hotel And Home Service Available Kolkata Call Girls South End Park ✔ 62971435...Hotel And Home Service Available Kolkata Call Girls South End Park ✔ 62971435...
Hotel And Home Service Available Kolkata Call Girls South End Park ✔ 62971435...
 
Verified Trusted Call Girls Tambaram Chennai ✔✔7427069034 Independent Chenna...
Verified Trusted Call Girls Tambaram Chennai ✔✔7427069034  Independent Chenna...Verified Trusted Call Girls Tambaram Chennai ✔✔7427069034  Independent Chenna...
Verified Trusted Call Girls Tambaram Chennai ✔✔7427069034 Independent Chenna...
 
Dakshineswar Call Girls ✔ 8005736733 ✔ Hot Model With Sexy Bhabi Ready For Se...
Dakshineswar Call Girls ✔ 8005736733 ✔ Hot Model With Sexy Bhabi Ready For Se...Dakshineswar Call Girls ✔ 8005736733 ✔ Hot Model With Sexy Bhabi Ready For Se...
Dakshineswar Call Girls ✔ 8005736733 ✔ Hot Model With Sexy Bhabi Ready For Se...
 
Independent Sonagachi Escorts ✔ 9332606886✔ Full Night With Room Online Booki...
Independent Sonagachi Escorts ✔ 9332606886✔ Full Night With Room Online Booki...Independent Sonagachi Escorts ✔ 9332606886✔ Full Night With Room Online Booki...
Independent Sonagachi Escorts ✔ 9332606886✔ Full Night With Room Online Booki...
 
Model Call Girls In Pazhavanthangal WhatsApp Booking 7427069034 call girl ser...
Model Call Girls In Pazhavanthangal WhatsApp Booking 7427069034 call girl ser...Model Call Girls In Pazhavanthangal WhatsApp Booking 7427069034 call girl ser...
Model Call Girls In Pazhavanthangal WhatsApp Booking 7427069034 call girl ser...
 
VIP Model Call Girls Budhwar Peth ( Pune ) Call ON 8005736733 Starting From 5...
VIP Model Call Girls Budhwar Peth ( Pune ) Call ON 8005736733 Starting From 5...VIP Model Call Girls Budhwar Peth ( Pune ) Call ON 8005736733 Starting From 5...
VIP Model Call Girls Budhwar Peth ( Pune ) Call ON 8005736733 Starting From 5...
 
𓀤Call On 6297143586 𓀤 Sonagachi Call Girls In All Kolkata 24/7 Provide Call W...
𓀤Call On 6297143586 𓀤 Sonagachi Call Girls In All Kolkata 24/7 Provide Call W...𓀤Call On 6297143586 𓀤 Sonagachi Call Girls In All Kolkata 24/7 Provide Call W...
𓀤Call On 6297143586 𓀤 Sonagachi Call Girls In All Kolkata 24/7 Provide Call W...
 
Zirakpur Call Girls👧 Book Now📱8146719683 📞👉Mohali Call Girl Service No Advanc...
Zirakpur Call Girls👧 Book Now📱8146719683 📞👉Mohali Call Girl Service No Advanc...Zirakpur Call Girls👧 Book Now📱8146719683 📞👉Mohali Call Girl Service No Advanc...
Zirakpur Call Girls👧 Book Now📱8146719683 📞👉Mohali Call Girl Service No Advanc...
 

Analysis and design of algorithms part 3

  • 1. G h R i dGraph Representation and Traversals Deepak John Department Of Computer Applications, SJCET-Pala
  • 2. Graph terminology - overviewp gy  A graph consists of t f ti V { }◦ set of vertices V = {v1, v2, ….. vn} ◦ set of edges that connect the vertices E ={e1, e2, …. em}  Two vertices in a graph are adjacent if there is an edgeg p j g connecting the vertices.  Two vertices are on a path if there is a sequences of vertices beginning with the first one and ending with the second onebeginning with the first one and ending with the second one  Graphs with ordered edges are directed. For directed graphs, vertices have in and out degrees. W i h d G h h l i d i h d Weighted Graphs have values associated with edges. Deepak John,Department Of IT,CE Poonjar
  • 3. Graph representation – undirectedp p h Adj li Adj igraph Adjacency list Adjacency matrix Graph representation – directed Deepak John,Department Of IT,CE Poonjar graph Adjacency list Adjacency matrix
  • 4. Adjacency Lists RepresentationAdjacency Lists Representation  A graph of n nodes is represented by a one-dimensional array L of linked lists, where,  L[i] is the linked list containing all the nodes adjacent from node i.  The nodes in the list L[i] are in no particular order Deepak John,Department Of IT,CE Poonjar
  • 5. Pros and Cons of Adjacency Matrices  P Pros:  Simple to implement  Easy and fast to tell if a pair (i,j) is an edge: simply check ify p ( ,j) g p y A[i][j] is 1 or 0  Cons:  No matter how few edges the graph has the matrix takes O(n2) No matter how few edges the graph has, the matrix takes O(n2) in memory Pros and Cons of Adjacency Lists  Pros:  Saves on space (memory): the representation takes as many memory words as there are nodes and edge.memory words as there are nodes and edge.  Cons:  It can take up to O(n) time to determine if a pair of nodes (i,j) is d ld h t h th li k d li t L[i] hi h Deepak John,Department Of IT,CE Poonjar an edge: one would have to search the linked list L[i], which takes time proportional to the length of L[i].
  • 6. Graph Traversal TechniquesGraph Traversal Techniques  There are two standard graph traversal techniques:  Depth First Search (DFS) Depth-First Search (DFS)  Breadth-First Search (BFS)  In both DFS and BFS, the nodes of the undirected graph are, g p visited in a systematic manner so that every node is visited exactly one.  Both BFS and DFS give rise to a tree: Both BFS and DFS give rise to a tree:  When a node x is visited, it is labeled as visited, and it is added to the tree If h l d f d i i d h If the traversal got to node x from node y, y is viewed as the parent of x, and x a child of y Deepak John,Department Of IT,CE Poonjar
  • 7. Depth-First SearchDepth-First Search  DFS follows the following rules: S l t i it d d i it it d t t th t1. Select an unvisited node x, visit it, and treat as the current node 2. Find an unvisited neighbor of the current node, visit it, andg make it the new current node; 3. If the current node has no unvisited neighbors, backtrack to the its parent and make that parent the new current node;the its parent, and make that parent the new current node; 4. Repeat steps 3 and 4 until no more nodes can be visited. 5. If there are still unvisited nodes, repeat from step 1. Deepak John,Department Of IT,CE Poonjar
  • 8. • It searches ‘deeper’ the graph when possible. • Starts at the selected node and explores as far as possible alongStarts at the selected node and explores as far as possible along each branch before backtracking. • Vertices go through white, gray and black stages of color. Whit i iti ll– White – initially – Gray – when discovered first – Black – when finished i.e. the adjacency list of the vertex isBlack when finished i.e. the adjacency list of the vertex is completely examined. • Also records timestamps for each vertex d[ ] h th t i fi t di d– d[v]when the vertex is first discovered – f[v] when the vertex is finished Deepak John,Department Of IT,CE Poonjar
  • 9.  Depth-first search: Strategy (for digraph)  choose a starting vertex, distance d = 0  vertices are visited in order of increasing distance from the starting vertex,  examine One edges leading from vertices (at distance d) to examine One edges leading from vertices (at distance d) to adjacent vertices (at distance d+1)  then, examine One edges leading from vertices at distance d+1 to distance d+2, and so on,  until no new vertex is discovered, or dead end  then backtrack one distance back up and try other edges and so then, backtrack one distance back up, and try other edges, and so on  until finally backtrack to starting vertex, with no more new vertex Deepak John,Department Of IT,CE Poonjar to be discovered.
  • 10. DFS(G) 1 for each vertex u ∈ V [G] 2 d l [ ] WHITE // l ll ti hit t th i t NIL2 do color[u] ← WHITE // color all vertices white, set their parents NIL 3 π[u] ← NIL 4 time ← 0 // zero out time 5 for each vertex u ∈ V [G] // call only for unexplored vertices5 for each vertex u ∈ V [G] // call only for unexplored vertices 6 do if color[u] = WHITE // this may result in multiple sources 7 then DFS-VISIT(u) DFS-VISIT(u) 1 color[u] ← GRAY ▹White vertex u has just been discovered. 2 time ← time +12 time ← time +1 3 d[u] time // record the discovery time 4 for each v ∈ Adj[u] ▹Explore edge(u, v). 5 do if color[v] = WHITE5 do if color[v] WHITE 6 then π[v] ← u // set the parent value 7 DFS-VISIT(v) // recursive call 8 color[u] BLACK ▹ Blacken u; it is finished Deepak John,Department Of IT,CE Poonjar 8 color[u] BLACK Blacken u; it is finished. 9 f [u] ▹ time ← time +1
  • 11.  forward edges- which point from a node of the tree to one of its descendants  back edges-which point from a node to one of its ancestors  cross edges, is any other edge in graph G. It connects vertices in two different DFS-tree or two vertices in the same DFS-tree neither of which is the ancestor of the other.  tree edges edges which belong to the spanning tree itself are tree edges, edges which belong to the spanning tree itself, are classified separately from forward edges Deepak John,Department Of IT,CE Poonjar
  • 12. Deepak John,Department Of IT,CE Poonjar
  • 13. Depth first search - analysisDepth first search analysis  Lines 1-3, initialization take time Θ(V).  Lines 5-7 take time Θ(V), excluding the time to call the DFS- VISITVISIT.  DFS-VISIT is called only once for each node (since it’s called only for white nodes and the first step in it is to paint the node )gray).  Loop on line 4-7 is executed |Adj(v)| times. Since, ∑vєV |Adj(v)| = Ө (E), the total cost of DFS-VISIT it θ(E) Th t t l t f DFS i θ(V+E)The total cost of DFS is θ(V+E) Deepak John,Department Of IT,CE Poonjar
  • 14. Breadth-First SearchBreadth First Search  BFS follows the following rules: 1 Select an unvisited node x visit it have it be the root in a BFS1. Select an unvisited node x, visit it, have it be the root in a BFS tree being formed. Its level is called the current level. 2. From each node z in the current level, in the order in which the level nodes were visited, visit all the unvisited neighbors of z. The newly visited nodes from this level form a new level that becomes the next current level. 3. Repeat step 2 until no more nodes can be visited. 4. If there are still unvisited nodes, repeat from Step 1. Deepak John,Department Of IT,CE Poonjar
  • 15. Breadth first search conceptsBreadth first search - concepts • To keep track of progress, it colors each vertex - white, gray or blackblack. • All vertices start white. • A vertex discovered first time during the search becomes hinonwhite. • All vertices adjacent to black ones are discovered. Whereas, gray ones may have some white adjacent vertices.y j • Gray represent the frontier between discovered and undiscovered vertices. Deepak John,Department Of IT,CE Poonjar
  • 16.  Breadth-first search: Strategy (for digraph)  choose a starting vertex, distance d = 0g ,  vertices are visited in order of increasing distance from the starting vertex,  examine all edges leading from vertices (at distance d) to examine all edges leading from vertices (at distance d) to adjacent vertices (at distance d+1)  then, examine all edges leading from vertices at distance d+1 t di t d+2 dto distance d+2, and so on,  until no new vertex is discovered  The predecessor of u is stored in the variable π[u]. The predecessor of u is stored in the variable π[u]. Deepak John,Department Of IT,CE Poonjar
  • 17. BFS - algorithm BFS(G, s) // G is the graph and s is the starting node 1 for each vertex u ∈ V [G] - {s} 2 do color[u] ← WHITE // color of vertex u[ ] 3 d[u] ← ∞ // distance from source s to vertex u 4 π[u] ← NIL // predecessor of u 5 color[s] ← GRAY 6 d[ ] 06 d[s] ← 0 7 π[s] ← NIL 8 Q ← Ø // Q is a FIFO - queue 9 ENQUEUE(Q, s)Q (Q, ) 10 while Q ≠ Ø // iterates as long as there are gray vertices. Lines 10-18 11 do u ← DEQUEUE(Q) 12 for each v ∈ Adj[u] 13 d if l [ ] WHITE // di h di d dj i13 do if color[v] = WHITE // discover the undiscovered adjacent vertices 14 then color[v] ← GRAY // enqueued whenever painted gray 15 d[v] ← d[u] + 1 16 π[v] ← u Deepak John,Department Of IT,CE Poonjar 16 π[v] u 17 ENQUEUE(Q, v) 18 color[u] ← BLACK // painted black whenever dequeued
  • 18. Deepak John,Department Of IT,CE Poonjar
  • 19. Breadth first search - analysis •The while-loop in breadth-first search is executed at most |V| times. The reason is that every vertex enqueued at most once. So,y q , we have O(V). •The for-loop inside the while-loop is executed at most |E| times if G is a directed graph or 2|E| times if G is undirected Theif G is a directed graph or 2|E| times if G is undirected. The reason is that every vertex dequeued at most once and we examine (u, v) only when u is dequeued. Therefore, every edge i d if di d i if di dexamined at most once if directed, at most twice if undirected. So, we have O(E). •Therefore, the total running time for breadth-first search, g traversal is O(V + E). Deepak John,Department Of IT,CE Poonjar
  • 20. STRONGLY CONNECTED COMPONENTS OF A DIRECTED GRAPHDIRECTED GRAPH  A directed graph is called strongly connected if there is a path from each vertex in the graph to every other vertex.  The strongly connected components of a directed graph G are its maximal strongly connected sub graphs. Deepak John,Department Of IT,CE Poonjar Graph with strongly connected components marked
  • 21. PropertiesProperties  Reflexive property: For all a, a # a. Any vertex is strongly connected to itself, by definition., y  Symmetric property: If a # b, then b # a. For strong connectivity, this follows from the symmetry of the definition. The same two th ( f t b d th f b t ) th t h th tpaths (one from a to b and another from b to a) that show that a ~ b, looked at in the other order (one from b to a and another from a to b) show that b ~ a.  Transitive property: If a # b and b # c, then a # c. Let's expand this out for strong connectivity: if a ~ b and b ~ c, we have four paths: a b b a b c and c b Concatenating them in pairs a b cpaths: a-b, b-a, b-c, and c-b. Concatenating them in pairs a-b-c and c-b-a produces two paths connecting a-c and c-a, so a ~ c, showing that the transitive property holds for strong connectivity. Deepak John,Department Of IT,CE Poonjar
  • 22. Algorithm to Find Strongly Connected ComponentAlgorithm to Find Strongly Connected Component  Strategy: Phase 1:  A standard depth-first search on G is performed, and the vertices are put in a stack at their finishing times Ph 2Phase 2:  A depth-first search is performed on GT, the transpose graph.  To start a search vertices are popped off the stack To start a search, vertices are popped off the stack.  A strongly connected component in the graph is identified by the name of its starting vertex (call leader). Deepak John,Department Of IT,CE Poonjar
  • 23. Deepak John,Department Of IT,CE Poonjar
  • 24. Deepak John,Department Of IT,CE Poonjar
  • 25. Deepak John,Department Of IT,CE Poonjar
  • 26. Bi-connected components of an Undirected graph  Biconnected graph:  A connected undirected graph G is said to b bi t d if it i t dbe biconnected if it remains connected after removal of any one vertex and the edges that are incident upon that vertex.  Bi t d t Biconnected component:  A biconnected component of a undirected graph is a maximal biconnected subgraph, that is a biconnected s bgraph notthat is, a biconnected subgraph not contained in any larger biconnected subgraph. Artic lation point:Artic lation points are theArticulation point:Articulation points are the points where the graph can be broken down into its biconnected components  C is an articulation point C is an articulation point Deepak John,Department Of IT,CE Poonjar
  • 27. Discovery of Biconnected Components via Articulation PointsArticulation Points If we can find articulation points then can compute biconnected components. Idea: • During DFS, use stack to store visited edges. E h ti l t th DFS f t hild f ti l ti• Each time we complete the DFS of a tree child of an articulation point, pop all stacked edges currently in stack • These popped off edges form a biconnected component.p pp g p Deepak John,Department Of IT,CE Poonjar
  • 28. Bi-connected components,  Undirected graph Deepak John,Department Of IT,CE Poonjar biconnected components
  • 29. Deepak John,Department Of IT,CE Poonjar
  • 30. Deepak John,Department Of IT,CE Poonjar
  • 31. Deepak John,Department Of IT,CE Poonjar
  • 32. WHAT IS BINARY RELATION  A binary relation R from the set S to the set T is a subset of S×T, R S×T. If S = T, we say that the relation is a binary l i Srelation on S.  P ti f Bi R l ti Properties of Binary Relation Let R be a binary relation on S.Then R is 1 Reflexive1. Reflexive 2. Symmetric 3 Anti-symmetric3. Anti-symmetric 4. Transitive Deepak John,Department Of IT,CE Poonjar
  • 33. Transitive closure of a graphTransitive closure of a graph The problem: Given a directed graph, G = (V, E), find all of the i h bl f i i Vvertices reachable from a given starting vertex v ϵV. Transitive closure (definition): Let G = (V E) be a graph where x RTransitive closure (definition): Let G = (V, E) be a graph, where x R y, y R z (x, y, z ϵ V). Then we can add a new edge x R z. A graph containing all of the edges of this nature is called the transitive closure of the original graph. The best way to represent the transitive closure graph (TCG) is byThe best way to represent the transitive closure graph (TCG) is by means of an adjacency matrix. Deepak John,Department Of IT,CE Poonjar
  • 34.  Consider the following adjacency matrix on the left representing a directed graph, the transitive closure is given on the right illustratingg p , g g g which vertices can reach other vertices •there is an edge from a to b and e b d b d•there is an edge from a to b and e. •b can reach d •d can reach c h ll i a b c d e a b c d e a 0 1 0 0 1 1 1 1 1 1 b 0 0 0 1 0 0 1 1 1 0 •a can reach all vertices. •But b cannot reach a c 0 1 0 0 0 0 1 1 1 0 d 0 0 1 0 0 0 1 1 1 0 e 0 0 0 1 0 0 1 1 1 1e 0 0 0 1 0 0 1 1 1 1 Deepak John,Department Of IT,CE Poonjar
  • 35. Strategy for Transitive ClosureStrategy for Transitive Closure  We noted earlier that if there is an path from a to b and from b to c, then there is a path from a to c  Our strategy for deriving a transitive closure matrix will be based on this simple idea  Start with a, compare it against each other vertex and see if there is, p g an edge  if so, the corresponding matrix value is true  if not see if there is already a path known from some vertex c to if not, see if there is already a path known from some vertex c to b and an edge from a to b, if so, then we know that there is a path from a to b Thi ill i th f 3 t d f l f th t ti This will require the use of 3 nested for-loops, one for the starting vertex of a path, one for the destination vertex of a path, and one to see if a path already exists from start to this point and from this i d i i Deepak John,Department Of IT,CE Poonjar point to destination
  • 36. It implies the following rules for generating R(k) from R(k-1): RR((kk))[[i ji j]] == RR((kk--11))[[i ji j]] oror ((RR((kk--11))[[i ki k]] andand RR((kk--11))[[k jk j])])RR(( ))[[i,ji,j]] RR(( ))[[i,ji,j]] oror ((RR(( ))[[i,ki,k]] andand RR(( ))[[k,jk,j])])  Rule 1 If an element in row i and column j is 1 in R(k-1),it remains 1 in R(k)  Rule 2 If an element in row i and column j is 0 in R(k-1), it has to be changed to 1 in R(k) if and only if the element in its row i and columnchanged to 1 in R(k) if and only if the element in its row i and column k and the element in its column j and row k are both 1’s in R(k-1)  Constructs transitive closure T as the last matrix in the sequence of n-by-n matrices R(0), … , R(k), … , R(n)  Note that R(0) = A (adjacency matrix), R(n) = T (transitive closure) Deepak John,Department Of IT,CE Poonjar
  • 38.  It should be obvious that the complexity is O(n3) because of the 3 nested for-loops.  The result is an NxN matrix where entry R[i, j] is true if there is a path from vertex i to vertex j.  The algorithm will work on either undirected or directed The algorithm will work on either undirected or directed graphs
  • 39. Warshall’s Algorithm (example) 3 1 3 1 3 1 42 0 0 1 0 42 42 0 0 1 0 1 0 0 1 0 0 0 0R(0) = 0 0 1 0 1 0 1 1 0 0 0 0R(1) = 0 0 1 0 1 1 1 1 0 0 0 0R(2) = 0 1 0 0 0 1 0 0 0 0 0 0 1 1 1 1 33 4 1 3 1 0 0 1 0 1 1 1 1 42 0 0 1 0 1 1 1 1 42 Deepak John,Department Of IT,CE Poonjar 1 1 1 1 0 0 0 0 1 1 1 1 R(3) = 1 1 1 1 0 0 0 0 1 1 1 1 R(4) =
  • 40. All-Pairs Shortest PathsAll Pairs Shortest Paths  Given a weighted graph G(V,E,w), the all-pairs shortest paths problem is to find the shortest paths between all pairs of vertices vi, vj ∈ V.  A number of algorithms are known for solving this problem A number of algorithms are known for solving this problem. FLOYD’S ALGORITHM: ALL PAIRS SHORTEST PATHSFLOYD’S ALGORITHM: ALL PAIRS SHORTEST PATHS Problem: In a weighted (di)graph, find shortest paths between every pair of vertices Same idea: construct solution through series of matrices D(0), …,D (n) Deepak John,Department Of IT,CE Poonjar
  • 41. Time efficiency: Θ(n3) Space efficiency: Matrices can be written over their predecessors Deepak John,Department Of IT,CE Poonjar Space efficiency: Matrices can be written over their predecessors
  • 42. Floyd’s Algorithm (example) 0 ∞ 3 ∞ 2 0 0 ∞ 3 ∞ 2 0 5 21 2 2 0 ∞ ∞ ∞ 7 0 1 6 ∞ ∞ 0 D(0) = 2 0 5 ∞ ∞ 7 0 1 6 ∞ 9 0 D(1) = 3 1 3 6 7 4 0 3 0 10 3 4 0 10 3 4 1 0 ∞ 3 ∞ 2 0 5 ∞ 9 7 0 1D(2) = 0 10 3 4 2 0 5 6 9 7 0 1D(3) = 0 10 3 4 2 0 5 6 7 7 0 1D(4) = 6 ∞ 9 0 6 16 9 0 6 16 9 0 Deepak John, Department Of IT,CE Poonjar
  • 43. Dynamic Programmingy g g  Dynamic Programming is an algorithm design technique for optimization problems: often minimizing or maximizing.p p g g  Like divide and conquer, DP solves problems by combining solutions to sub problems. U lik di id d b bl t i d d t Unlike divide and conquer, sub problems are not independent.  Sub problems may share subsubproblems,  However, solution to one sub problem may not affect the solutions to other sub bl f h blproblems of the same problem.  DP reduces computation by  Solving sub problems in a bottom-up fashion.g p p  Storing solution to a sub problem the first time it is solved.  Looking up the solution when sub problem is encountered again.  Key: determine structure of optimal solutions Deepak John,Department Of IT,CE Poonjar  Key: determine structure of optimal solutions
  • 44. Steps in Dynamic Programming 1 Characterize structure of an optimal solution1. Characterize structure of an optimal solution. 2. Define value of optimal solution recursively. 3. Compute optimal solution valuesp p 4. Construct an optimal solution from computed values. Elements of Dynamic Programming  Optimal substructure  Overlapping subproblems Deepak John,Department Of IT,CE Poonjar
  • 45. Optimal Binary Search TreesOptimal Binary Search Trees  OBST is one special kind of advanced tree.  It focus on how to reduce the cost of the search of the BST It focus on how to reduce the cost of the search of the BST.  A good example of a dynamic algorithm • Solves all the small problems ild l i l bl f h• Builds solutions to larger problems from them • Requires space to store small problem results Deepak John,Department Of IT,CE Poonjar
  • 46.  Problem  Given sequence K = k1 < k2 <··· < kn of n sorted keys, with aq 1 2 n y , search probability pi for each key ki.  Want to build a binary search tree (BST) with minimum expected search costwith minimum expected search cost.  Actual cost = number of items examined.  For key ki, cost = depthT(ki)+1, where For key ki, cost depthT(ki) 1, where depthT(ki) = depth of ki in BST T . TE ]incostsearch[   n i iiT pk TE 1 )(depth1 ]incostsearch[ i 1
  • 47.  Consider 5 keys with these search probabilities: p1 = 0.25, p2 = 0.2, p3 = 0.05, p4 = 0.2, p5 = 0.3. k2 i depthT(ki) depthT(ki)·pi 1 1 0 25 k1 k4 1 1 0.25 2 0 0 3 2 0.1 4 1 0 2 k3 k5 4 1 0.2 5 2 0.6 1.15 k3 k5 Therefore, [search cost] = 2.15. Deepak John,Department Of IT,CE Poonjar
  • 48.  p1 = 0.25, p2 = 0.2, p3 = 0.05, p4 = 0.2, p5 = 0.3. i depthT(ki) depthT(ki)·pi 1 1 0.25 2 0 0 k2 2 0 0 3 3 0.15 4 2 0.4 5 1 0 3 k1 k5 5 1 0.3 1.10 k4 Therefore, E[search cost] = 2.10. k3 This tree turns out to be optimal for this set of keys Deepak John,Department Of IT,CE Poonjar 3 This tree turns out to be optimal for this set of keys.
  • 49. Optimal Substructure  Any sub tree of a BST contains keys in a contiguous range ki, ..., kj Tj T T’  If T is an optimal BST and T contains sub tree T’ with keys ki, ... ,kj , then T’must be an optimal BST for keys ki, ..., kj. Deepak John,Department Of IT,CE Poonjar , j , p y i, , j 1
  • 50. Pseudo-code OPTIMAL-BST(p, q, n)OPTIMAL-BST(p, q, n)(p q ) 1. for i ← 1 to n + 1 2. do e[i, i 1] ← 0 3. w[i, i 1] ← 0 (p q ) 1. for i ← 1 to n + 1 2. do e[i, i 1] ← 0 3. w[i, i 1] ← 0 4. for l ← 1 to n 5. do for i ← 1 to nl + 1 6. do j ←i + l1 4. for l ← 1 to n 5. do for i ← 1 to nl + 1 6. do j ←i + l1 7. e[i, j ]←∞ 8. w[i, j ] ← w[i, j1] + pj 9. for r ←i to j 7. e[i, j ]←∞ 8. w[i, j ] ← w[i, j1] + pj 9. for r ←i to j 10. do t ← e[i, r1] + e[r + 1, j ] + w[i, j ] 11. if t < e[i, j ] 12. then e[i, j ] ← t 10. do t ← e[i, r1] + e[r + 1, j ] + w[i, j ] 11. if t < e[i, j ] 12. then e[i, j ] ← t 13. root[i, j ] ←r 14. return e and root 13. root[i, j ] ←r 14. return e and root