Merge Sort Using Divide and Conquer
Merge Sort Using Divide and Conquer
// Driver method
public static void main(String args[])
{
int arr[] = {12, 11, 13, 5, 6, 7};
System.out.println("Given Array");
printArray(arr);
System.out.println("\nSorted array");
printArray(arr);
}
}
import java.util.*;
int i,j,min=999,num_edges=1,a=1,b=1,minpos_i=1,minpos_j=1;
while(num_edges < n)
for(i=1,min=999;i<=n;i++)
for(j=1;j<=n;j++)
if(this.cost[i][j]<min)
if(this.isVisited[i]!=0)
min=this.cost[i][j];
a=minpos_i=i;
b=minpos_j=j;
if(this.isVisited[minpos_i]==0 || this.isVisited[minpos_j]==0)
num_edges=num_edges+1;
this.isVisited[b]=1;
this.cost[a][b]=this.cost[b][a]=999;
int nodes,i,j;
nodes = in.nextInt();
for(i=1;i<=nodes;i++)
for(j=1;j<=nodes;j++)
{
p.cost[i][j]=in.nextInt();
if(p.cost[i][j]==0)
p.cost[i][j]=999;
p.isVisited[1]=1; // Initialization
p.calc(nodes);
// Constructor
Graph(int v)
{
V = v;
adj = new LinkedList[v];
for (int i=0; i<v; ++i)
adj[i] = new LinkedList();
}
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);
g.DFS(2);
}
}
// This code is contributed by Aakash Hasija
http://campuscoke.blogspot.com/2015/01/distance-vector-
routing-dvr-algorithm.html
import java.io.*;
public class DVR
{
static int graph[][];
static int via[][];
static int rt[][];
static int v;
static int e;
System.out.print("Please enter the Source Node for the edge whose cost has
changed: ");
int s = Integer.parseInt(br.readLine());
s--;
System.out.print("Please enter the Destination Node for the edge whose cost
has changed: ");
int d = Integer.parseInt(br.readLine());
d--;
System.out.print("Please enter the new cost: ");
int c = Integer.parseInt(br.readLine());
graph[s][d] = c;
graph[d][s] = c;
}
output:-
Please enter the Source Node for the edge whose cost has changed: 2
Please enter the Destination Node for the edge whose cost has changed: 4
Please enter the new cost: 10
Dijkstra's algorithm
import java.util.Scanner; //Scanner Function to take in the Input Values
visited[nextNode] = 1;
for (int i = 0; i < 5; i++)
{
if (visited[i]!=1)
{
if (min+matrix[nextNode][i] < distance[i])
{
distance[i] = min+matrix[nextNode][i];
preD[i] = nextNode;
}
System.out.println("|");
int j;
for (int i = 0; i < 5; i++)
{
if (i!=0)
{