28 Sept 2013

Bellman - Ford Algorithm

                                                  Bellman - Ford Algorithm


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import java.util.ArrayList;
import java.util.List;
/**
 * Edge of Graph
 * @author Prateek
 */
class Edge {
 int src;
 int dest;
 int weight;

 public Edge(int src,int dest,int weight) {
  this.src=src;
  this.dest=dest;
  this.weight=weight;
 }

 @Override
 public String toString() {
  return "Edge: "+src+ " - " + dest + " | " +"  Weight: "+ weight;
 }
}

/**
 * Bellman's Algorithm for Single Source Shortest Path
 * @author Prateek
 *
 */
 class BellmanFord {
 
 private static final int MAX_INF=999999;

 private int mNumVertices;   // Number of vertices in the graph
 private int mNumEdges;  // Number of edges in the graph
 
 // Input Edge List
 private List<Edge> mEdgeList;  

 public BellmanFord(int numVertices,int numEdges) {
  this.mNumVertices=numVertices;
  this.mNumEdges=numEdges;
  
  this.mEdgeList=new ArrayList<Edge>(numEdges);
 }
 
 private  boolean bellmanFord(int src)
 {
  // maintains the updated distance
  int[] dist=new int[mNumVertices];
  
  //Initialisation
  for(int i=0;i<mNumVertices ; i++) 
   dist[i] = MAX_INF;
  dist[src]=0;
  
  for(int i=0;i<= mNumVertices ; i++)
  {
   for(int j=0;j< mNumEdges ; j++) 
   {
    Edge edge=mEdgeList.get(j);
    int u=edge.src;
    int v=edge.dest;
    int weight=edge.weight;
    
    // Relaxing the edge
    if(dist[v] > dist[u]+weight)
     dist[v]=dist[u]+weight;
   }
  }
  
  //check for negative cycles, by inspecting the distance of all vertices
  // if we find any dist[v] greater distance than dist[u]+ weight, that means 
  // we have already cycled through some edge more than once
  for(int i=0 ;i< mNumEdges ;i++)
  {
   Edge edge=mEdgeList.get(i);
   int u=edge.src;
   int v=edge.dest;
   int weight=edge.weight;
   
   if(dist[u]+weight < dist[v])
   {
    System.out.println("Undefined Graph , Shortest path cant be calculated");
    return false;
   }
  }
   
  printShortestPath(src , dist);
  return true;
 }
 
 private void printShortestPath(int src , int[] dist) {
  for(int i=0;i<mNumVertices ;i++)
   System.out.println(src + " ---> " + i + "    "   +dist[i]);
 }
 
 public static void main(String[] args) {
  BellmanFord graph=new BellmanFord(5, 6);
  graph.helper();
  
 }
 
 public void helper() {
  
  mEdgeList.add(new Edge(0, 1, 2));
  mEdgeList.add(new Edge(0, 2, 4));
  
  mEdgeList.add(new Edge(1, 3, 2));
  mEdgeList.add(new Edge(1, 2, 1));
  
  mEdgeList.add(new Edge(2, 4, 4));
  
  mEdgeList.add(new Edge(3, 4, 2));
  
  int sourceVertex = 0;
  bellmanFord(sourceVertex);
 }
}


References: http://faculty.ycp.edu/~dbabcock/PastCourses/cs360/lecture/lecture21.html

2 comments:

  1. Вy way of example, to get the lowest $2,000 short-term mortgsge loan fom Washington Mutual, уou
    might want а credit worthiness of at lleast 650 On top oof that, thеre may
    be pensions and other fringe benefits to that you or your loved
    one may be permittеd, so you want to you should definitely choose one of the most useful divorce lawyers
    to guarantee protectiߋn ooff your possessions, government and civilian This qualifications will be simple and the
    credit сheck jut isn't performed Unless of coourse you could paү off the еntire
    balancе each month, аs compared with having this unit card could be very useful with theiг incеntiveѕ program
    Operɑting Sрending budɡeet ' Thіs is the company's pre-planned
    expenses as a means connected with controlling fees and decreasing the incurrence associated with eҳpendituгes іnside the amount estimɑteԁ or
    structurеd asѕ acϲeptable These can be the geography,
    distribution related or evеn of any different kind What most of the people may carry out, iѕ to transfeг tҺеir
    сhargе card balances in one 0 APR crеdit card to an alernative before every single
    expiring period, the situation with thіs system though сan be,
    there is no guаrantee you will eep on getting 4 APR cards approvalѕ,
    provided you need them,аnd just trаnsferring your creԀit card dents around,
    without paying on your debts, could affect your own approval in a negative
    way for other kіnds of credits sսcҺ as a
    car or home loan, using yoսr creditors Thee main іntent behind this ɑspect is to make
    certain tҺat the orǥanizational means are used insiude a most efficіent way and preventative measure
    of dailyy ѕupervision 35, Chillier weather which will creates eҳcellent
    snuggle temperature While using the assistance off cash advance loa without
    immediate deposit system, lndeгs hlp you obtain speedy fund
    thaat will ranges by $100 to $1500 dependant on yօr transaction capability You need to haѵе a bank acсount in good ѕtanding along with a stedady income tο ߋbtain dollars till a person's salаry niight out
    Also, to avid jսst about any unwanted circumstances, dress in an increasingly local approach; however
    for some that you neeed to camo or ought tto look like a nearby Respndents towards ity sensitive will be liable foг
    ensսring the paгticular vehiϲles are distrіbuted corrfectly Νow carry out your numerous
    expenses wіth virtuallƴ no restrictions іncluding сar vehicle repairs, medical ƅіlls, householԀ eхpense,
    little one's education rate and so forgh Maintaaining eveгy biit of
    those offeres through provider credit card manage could be a
    hatɗ ctivity Larger lending prodսcts will certainly take longer to repay, particularly iff you have a number of
    monthѕ to move You can use the totfal amount for dealing wih your current vvarious requires like domestic or private Еven whеn yyou want to obtain a loan prtoper at your ease home οwing tto your firm job plan, then doorstep
    collеction lending options will bee a great alternatiѵe to create funds with hasslе free approacҺ No paperwork is іnvolved in 1 yrar loans with rеgard tto unemployed therefore гelaxing via faxig rеgarding documents If you want to accomplish morе detailed investigation on your accounts transactions, tis app allows
    you to doԝnlosd the transactions with Excel and also Quicken report
    extensions through еmail Youu mսѕt make up your mind to receive that loɑn since ƴou will not bе investing
    some otҺer penny thɑt you've got; instead you are attempting to get a
    financial loаn

    My page; payday loans bad credit

    ReplyDelete
  2. HELLO SIR,
    Can you write the code for manhattan algorithm and euclidean algorithm in Java?

    ReplyDelete