Sie sind auf Seite 1von 4

1.

Prims Algorithm
Algorithm
1) Create a set mstSet that keeps track of vertices already included in MST.
2) Assign a key value to all vertices in the input graph. Initialize all key values as INFINITE.
Assign key value as 0 for the first vertex so that it is picked first.
3) While mstSet doesn’t include all vertices
….a) Pick a vertex u which is not there in mstSet and has minimum key value.
….b) Include u to mstSet.
….c) Update key value of all adjacent vertices of u. To update the key values, iterate through all
adjacent vertices. For every adjacent vertex v, if weight of edge u-v is less than the previous key
value of v, update the key value as weight of u-v

2.Kruskal’s Minimum Spanning Tree Algorithm


1. Sort all the edges in non-decreasing order of their weight.
2. Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so far. If cycle
is not formed, include this edge. Else, discard it.
3. Repeat step#2 until there are (V-1) edges in the spanning tree.

3.Floyd Warshalls

for (k = 0; k < V; k++)


{
// Pick all vertices as source one by one
for (i = 0; i < V; i++)
{
// Pick all vertices as destination for the
// above picked source
for (j = 0; j < V; j++)
{
// If vertex k is on the shortest path from
// i to j, then update the value of dist[i][j]
if (dist[i][k] + dist[k][j] < dist[i][j])
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}

4.Bellman Ford Algorithm


Input: Graph and a source vertex src
Output: Shortest distance to all vertices from src. If there is a negative weight cycle, then
shortest distances are not calculated, negative weight cycle is reported.

1) This step initializes distances from source to all vertices as infinite and distance to source
itself as 0. Create an array dist[] of size |V| with all values as infinite except dist[src] where src is
source vertex.

2) This step calculates shortest distances. Do following |V|-1 times where |V| is the number of
vertices in given graph.
…..a) Do following for each edge u-v
………………If dist[v] > dist[u] + weight of edge uv, then update dist[v]
………………….dist[v] = dist[u] + weight of edge uv

3) This step reports if there is a negative weight cycle in graph. Do following for each edge u-v
……If dist[v] > dist[u] + weight of edge uv, then “Graph contains negative weight cycle”
The idea of step 3 is, step 2 guarantees shortest distances if graph doesn’t contain negative
weight cycle. If we iterate through all edges one more time and get a shorter path for any vertex,
then there is a negative weight cycle

Dijskatra
1)​ Create a set s
​ ptSet​ (shortest path tree set) that keeps track of vertices included in 

shortest path tree, i.e., whose minimum distance from source is calculated and 

finalized. Initially, this set is empty. 

2)​ Assign a distance value to all vertices in the input graph. Initialize all distance values 

as INFINITE. Assign distance value as 0 for the source vertex so that it is picked first. 

3)​ While ​sptSet​ doesn’t include all vertices 

….​a)​ Pick a vertex u which is not there in ​sptSet​ and has minimum distance value. 

….​b)​ Include u to ​sptSet​. 

….​c)​ Update distance value of all adjacent vertices of u. To update the distance values, 

iterate through all adjacent vertices. For every adjacent vertex v, if sum of distance value 

of u (from source) and weight of edge u-v, is less than the distance value of v, then 

update the distance value of v. 


5.Red-Black Tree | Set 1 (Introduction)
Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows
following rules.
RedBlackTree
1) Every node has a color either red or black.

2) Root of tree is always black.

3) There are no two adjacent red nodes (A red node cannot have a red parent or red child).

4) Every path from a node (including root) to any of its descendant NULL node has the same
number of black nodes.

Every Red Black Tree with n nodes has height <= 2Log2(n+1)

This can be proved using following facts:


1) For a general Binary Tree, let k be the minimum number of nodes on all root to NULL paths,
then n >= 2k – 1 (Ex. If k is 3, then n is atleast 7). This expression can also be written as k <=
Log2(n+1)

2) From property 4 of Red-Black trees and above claim, we can say in a Red-Black Tree with n
nodes, there is a root to leaf path with at-most Log2(n+1) black nodes.

3) From property 3 of Red-Black trees, we can claim that the number black nodes in a
Red-Black tree is at least ? n/2 ? where n is the total number of nodes.

From above 2 points, we can conclude the fact that Red Black Tree with n nodes has height <=
2Log2(n+1)

6.Matrix Chain Multiplication


l=2 to n
i=1 to n-l+1
j=i+l-1
k=i to j-1
q=m[i,k]+[k+1,j]+p(i-1)p(j)p(k)
if(q<m[i,j])
m[i,j]=q;
s[i,j]=k;

Das könnte Ihnen auch gefallen