Professional Documents
Culture Documents
Bellman-Ford: A. First Edition
Bellman-Ford: A. First Edition
Bellman-Ford
A. First Edition
This is just a small assignment from networking and the assignment is a
theory one. But I am so lazy to do the
numb-minded-mechanical job that I would rather my best friend, my laptop,
to do it for me.
B.The problem
Bellman-Ford is said to be a distributed algorithm for routing algorithm. The question is just
how the nodes in
a network find out the cheapest route to each other node. Considering the graph is an
undirected graph with each
vertex representing a router and with each edge attached with an integer cost. The bigger the
cost is, the
I use adjacency matrix to represent the graph with each entry as the cost. If two vertices are not adjacent,
the
cost is "INFINITE" which is just -1. The following is the input matrix. However, at last I changed the
0 3 -1 3 -1 -1 -1
3 0 2 -1 -1 -1 -1
-1 2 0 2 2 -1 -1
3 -1 2 0 6 -1 -1
-1 -1 2 6 0 4 3
-1 -1 -1 -1 4 0 -1
-1 -1 -1 -1 3 -1 0
The logic of Bellman-Ford algorithm is rather intuitive and simple. It is like people's exchanging
information.
i.e. People go to pub and talk and exchange information and compare with his old knowledge. When he
find a new
route, he updates his routing table. However, read the graph is a bit boring, so, I borrow my old code---
matrix
class Matrix
{
private:
int rowNum;
int colNum;
double lst[MaxRow][MaxCol];
protected:
void mul(int dest, double scalor);
void mul(int source, int dest, double scalor);
public:
Matrix();
Matrix& transform(int index1, int index2);
int row() const {return rowNum;}
int col() const {return colNum;}
void setRow(const int newRow) { rowNum = newRow;}
void setCol(const int newCol) { colNum = newCol;}
void display(bool displayNumber= false);
double& items(int r, int c);
void initialize();
void readFromFile(const char* fileName);
void assign(const Matrix& other);
Matrix& operator = (Matrix& other);
Matrix& transposition();
bool operator==(Matrix& other);
bool operator!=(Matrix& other);
double* operator[](int r);
};
#endif
file name: matrix.cpp
#include <iostream>
#include <cmath>
#include <fstream>
#include "matrix.h"
using namespace std;
Matrix& Matrix::transposition()
{
double hold;
int temp;
for (int r =0; r< rowNum; r++)
{
for (int c=0; c< r; c++)
{
hold = lst[r][c];
lst[r][c] = lst[c][r];
lst[c][r] = hold;
}
}
temp = rowNum;
rowNum = colNum;
colNum = temp;
return (*this);
}
return (*this);
}
f>>lst[r][c];
c++;
if (c>colNum)
colNum = c;
}
else
{
f.ignore();
r++;
setCol(c);
c =0;
}
}
if (r!=0)
{
setRow(r+1);
}
}
void Matrix::initialize()
{
for (int r=0; r < rowNum; r++)
{
for (int c=0; c< colNum; c++)
{
lst[r][c] = r*2+c;
}
}
}
cout<<"\nrow\\col";
for (int c=0; c< colNum; c++)
{
cout<<" "<<c;
}
cout<<"\n\n";
for (int r = 0; r< rowNum; r++)
{
cout<<r<<"\t ";
number = 0;
for (c = 0; c< colNum; c++)
{
cout<<(fabs(lst[r][c])<LIMIT?0:lst[r][c])<<" ";
if (fabs(lst[r][c])>=LIMIT)
{
number++;
}
}
if (displayNumber)
{
cout<<number;
}
cout<<endl;
}
// cout.precision(temp);
cout.flags(preFlag);
}
Matrix::Matrix()
{
rowNum = 5;
colNum = 5;
initialize();
}
file name: BellmanFord.cpp
#include <stdio.h>
#include <stdlib.h>
#include "matrix.h"
struct Node
{
int index;
int cost;
};
class Bellman
{
private:
Matrix matrix;
Node nodeMatrix[MaxVertex][MaxVertex];
int row, col;
void update(int r, int c, int first);
public:
void readFile(char* fileName);
void display();
void displayMatrix();
bool findCost(int r, int c);
void doFindCost();
};
int main()
{
Bellman B;
B.readFile("nick.txt");
B.display();
B.doFindCost();
//B.displayMatrix();
B.display();
return 0;
}
void Bellman::doFindCost()
{
bool findNew;
do
{
findNew=false;
for (int r=0; r<row; r++)
{
for (int c=0; c<col; c++)
{
if (findCost(r, c))
{
//display();
printf("array[%d][%d]\n", r, c);
findNew=true;
}
}
display();
}
}
while (findNew);
}
}
void Bellman::display()
{
for (int i=0; i<row; i++)
{
printf("\t%c", i+'A');
}
printf("\n------------------------------\n");
void Bellman::displayMatrix()
{
matrix.display();
}
How to run?
1. You need the graph input matrix file---"nick.txt" which is like
following:
0 3 -1 3 -1 -1 -1
3 0 2 -1 -1 -1 -1
-1 2 0 2 2 -1 -1
3 -1 2 0 6 -1 -1
-1 -1 2 6 0 4 3
-1 -1 -1 -1 4 0 -1
-1 -1 -1 -1 3 -1 0
2. I give two output version: one is a debug-version which shows each
iteration the matrix discover new route;
the other is just the start and end matrix.
3. This the final clean output version:
A B C D E F G
------------------------------
A (A,0) (B,3) (-,-) (D,3) (-,-) (-,-) (-,-)
B (A,3) (B,0) (C,2) (-,-) (-,-) (-,-) (-,-)
C (-,-) (B,2) (C,0) (D,2) (E,2) (-,-) (-,-)
D (A,3) (-,-) (C,2) (D,0) (E,6) (-,-) (-,-)
E (-,-) (-,-) (C,2) (D,6) (E,0) (F,4) (G,3)
F (-,-) (-,-) (-,-) (-,-) (E,4) (F,0) (-,-)
G (-,-) (-,-) (-,-) (-,-) (E,3) (-,-) (G,0)
A B C D E F G
------------------------------
A (A,0) (B,3) (B,5) (D,3) (B,7) (D,11) (D,10)
B (A,3) (B,0) (C,2) (C,4) (C,4) (C,8) (C,7)
C (B,5) (B,2) (C,0) (D,2) (E,2) (E,6) (E,5)
D (A,3) (C,4) (C,2) (D,0) (C,4) (C,8) (C,7)
E (C,7) (C,4) (C,2) (C,4) (E,0) (F,4) (G,3)
F (E,11) (E,8) (E,6) (E,8) (E,4) (F,0) (E,7)
G (E,10) (E,7) (E,5) (E,7) (E,3) (E,7) (G,0)
A B C D E F G
------------------------------
A (A,0) (B,3) (-,-) (D,3) (-,-) (-,-) (-,-)
B (A,3) (B,0) (C,2) (-,-) (-,-) (-,-) (-,-)
C (-,-) (B,2) (C,0) (D,2) (E,2) (-,-) (-,-)
D (A,3) (-,-) (C,2) (D,0) (E,6) (-,-) (-,-)
E (-,-) (-,-) (C,2) (D,6) (E,0) (F,4) (G,3)
F (-,-) (-,-) (-,-) (-,-) (E,4) (F,0) (-,-)
G (-,-) (-,-) (-,-) (-,-) (E,3) (-,-) (G,0)
array[0][2]
array[0][4]
array[0][5]
array[0][6]
ABCDEFG
------------------------------
A (A,0) (B,3) (B,5) (D,3) (B,7) (B,11) (B,10)
B (A,3) (B,0) (C,2) (-,-) (C,4) (C,8) (C,7)
C (-,-) (B,2) (C,0) (D,2) (E,2) (E,6) (E,5)
D (A,3) (-,-) (C,2) (D,0) (E,6) (-,-) (-,-)
E (-,-) (-,-) (C,2) (D,6) (E,0) (F,4) (G,3)
F (-,-) (-,-) (-,-) (-,-) (E,4) (F,0) (-,-)
G (-,-) (-,-) (-,-) (-,-) (E,3) (-,-) (G,0)
array[1][3]
ABCDEFG
------------------------------
A (A,0) (B,3) (B,5) (D,3) (B,7) (B,11) (B,10)
B (A,3) (B,0) (C,2) (C,4) (C,4) (C,8) (C,7)
C (-,-) (B,2) (C,0) (D,2) (E,2) (E,6) (E,5)
D (A,3) (-,-) (C,2) (D,0) (E,6) (-,-) (-,-)
E (-,-) (-,-) (C,2) (D,6) (E,0) (F,4) (G,3)
F (-,-) (-,-) (-,-) (-,-) (E,4) (F,0) (-,-)
G (-,-) (-,-) (-,-) (-,-) (E,3) (-,-) (G,0)
array[2][0]
ABCDEFG
------------------------------
A (A,0) (B,3) (B,5) (D,3) (B,7) (B,11) (B,10)
B (A,3) (B,0) (C,2) (C,4) (C,4) (C,8) (C,7)
C (B,5) (B,2) (C,0) (D,2) (E,2) (E,6) (E,5)
D (A,3) (-,-) (C,2) (D,0) (E,6) (-,-) (-,-)
E (-,-) (-,-) (C,2) (D,6) (E,0) (F,4) (G,3)
F (-,-) (-,-) (-,-) (-,-) (E,4) (F,0) (-,-)
G (-,-) (-,-) (-,-) (-,-) (E,3) (-,-) (G,0)
array[3][1]
array[3][4]
array[3][5]
array[3][6]
ABCDEFG
------------------------------
A (A,0) (B,3) (B,5) (D,3) (B,7) (B,11) (B,10)
B (A,3) (B,0) (C,2) (C,4) (C,4) (C,8) (C,7)
C (B,5) (B,2) (C,0) (D,2) (E,2) (E,6) (E,5)
D (A,3) (C,4) (C,2) (D,0) (C,4) (C,8) (C,7)
E (-,-) (-,-) (C,2) (D,6) (E,0) (F,4) (G,3)
F (-,-) (-,-) (-,-) (-,-) (E,4) (F,0) (-,-)
G (-,-) (-,-) (-,-) (-,-) (E,3) (-,-) (G,0)
array[4][0]
array[4][1]
array[4][3]
ABCDEFG
------------------------------
A (A,0) (B,3) (B,5) (D,3) (B,7) (B,11) (B,10)
B (A,3) (B,0) (C,2) (C,4) (C,4) (C,8) (C,7)
C (B,5) (B,2) (C,0) (D,2) (E,2) (E,6) (E,5)
D (A,3) (C,4) (C,2) (D,0) (C,4) (C,8) (C,7)
E (C,7) (C,4) (C,2) (C,4) (E,0) (F,4) (G,3)
F (-,-) (-,-) (-,-) (-,-) (E,4) (F,0) (-,-)
G (-,-) (-,-) (-,-) (-,-) (E,3) (-,-) (G,0)
array[5][0]
array[5][1]
array[5][2]
array[5][3]
array[5][6]
ABCDEFG
------------------------------
A (A,0) (B,3) (B,5) (D,3) (B,7) (B,11) (B,10)
B (A,3) (B,0) (C,2) (C,4) (C,4) (C,8) (C,7)
C (B,5) (B,2) (C,0) (D,2) (E,2) (E,6) (E,5)
D (A,3) (C,4) (C,2) (D,0) (C,4) (C,8) (C,7)
E (C,7) (C,4) (C,2) (C,4) (E,0) (F,4) (G,3)
F (E,11) (E,8) (E,6) (E,8) (E,4) (F,0) (E,7)
G (-,-) (-,-) (-,-) (-,-) (E,3) (-,-) (G,0)
array[6][0]
array[6][1]
array[6][2]
array[6][3]
array[6][5]
ABCDEFG
------------------------------
A (A,0) (B,3) (B,5) (D,3) (B,7) (B,11) (B,10)
B (A,3) (B,0) (C,2) (C,4) (C,4) (C,8) (C,7)
C (B,5) (B,2) (C,0) (D,2) (E,2) (E,6) (E,5)
D (A,3) (C,4) (C,2) (D,0) (C,4) (C,8) (C,7)
E (C,7) (C,4) (C,2) (C,4) (E,0) (F,4) (G,3)
F (E,11) (E,8) (E,6) (E,8) (E,4) (F,0) (E,7)
G (E,10) (E,7) (E,5) (E,7) (E,3) (E,7) (G,0)
ABCDEFG
------------------------------
A (A,0) (B,3) (B,5) (D,3) (B,7) (B,11) (B,10)
B (A,3) (B,0) (C,2) (C,4) (C,4) (C,8) (C,7)
C (B,5) (B,2) (C,0) (D,2) (E,2) (E,6) (E,5)
D (A,3) (C,4) (C,2) (D,0) (C,4) (C,8) (C,7)
E (C,7) (C,4) (C,2) (C,4) (E,0) (F,4) (G,3)
F (E,11) (E,8) (E,6) (E,8) (E,4) (F,0) (E,7)
G (E,10) (E,7) (E,5) (E,7) (E,3) (E,7) (G,0)
ABCDEFG
------------------------------
A (A,0) (B,3) (B,5) (D,3) (B,7) (B,11) (B,10)
B (A,3) (B,0) (C,2) (C,4) (C,4) (C,8) (C,7)
C (B,5) (B,2) (C,0) (D,2) (E,2) (E,6) (E,5)
D (A,3) (C,4) (C,2) (D,0) (C,4) (C,8) (C,7)
E (C,7) (C,4) (C,2) (C,4) (E,0) (F,4) (G,3)
F (E,11) (E,8) (E,6) (E,8) (E,4) (F,0) (E,7)
G (E,10) (E,7) (E,5) (E,7) (E,3) (E,7) (G,0)
ABCDEFG
------------------------------
A (A,0) (B,3) (B,5) (D,3) (B,7) (B,11) (B,10)
B (A,3) (B,0) (C,2) (C,4) (C,4) (C,8) (C,7)
C (B,5) (B,2) (C,0) (D,2) (E,2) (E,6) (E,5)
D (A,3) (C,4) (C,2) (D,0) (C,4) (C,8) (C,7)
E (C,7) (C,4) (C,2) (C,4) (E,0) (F,4) (G,3)
F (E,11) (E,8) (E,6) (E,8) (E,4) (F,0) (E,7)
G (E,10) (E,7) (E,5) (E,7) (E,3) (E,7) (G,0)
ABCDEFG
------------------------------
A (A,0) (B,3) (B,5) (D,3) (B,7) (B,11) (B,10)
B (A,3) (B,0) (C,2) (C,4) (C,4) (C,8) (C,7)
C (B,5) (B,2) (C,0) (D,2) (E,2) (E,6) (E,5)
D (A,3) (C,4) (C,2) (D,0) (C,4) (C,8) (C,7)
E (C,7) (C,4) (C,2) (C,4) (E,0) (F,4) (G,3)
F (E,11) (E,8) (E,6) (E,8) (E,4) (F,0) (E,7)
G (E,10) (E,7) (E,5) (E,7) (E,3) (E,7) (G,0)
ABCDEFG
------------------------------
A (A,0) (B,3) (B,5) (D,3) (B,7) (B,11) (B,10)
B (A,3) (B,0) (C,2) (C,4) (C,4) (C,8) (C,7)
C (B,5) (B,2) (C,0) (D,2) (E,2) (E,6) (E,5)
D (A,3) (C,4) (C,2) (D,0) (C,4) (C,8) (C,7)
E (C,7) (C,4) (C,2) (C,4) (E,0) (F,4) (G,3)
F (E,11) (E,8) (E,6) (E,8) (E,4) (F,0) (E,7)
G (E,10) (E,7) (E,5) (E,7) (E,3) (E,7) (G,0)
ABCDEFG
------------------------------
A (A,0) (B,3) (B,5) (D,3) (B,7) (B,11) (B,10)
B (A,3) (B,0) (C,2) (C,4) (C,4) (C,8) (C,7)
C (B,5) (B,2) (C,0) (D,2) (E,2) (E,6) (E,5)
D (A,3) (C,4) (C,2) (D,0) (C,4) (C,8) (C,7)
E (C,7) (C,4) (C,2) (C,4) (E,0) (F,4) (G,3)
F (E,11) (E,8) (E,6) (E,8) (E,4) (F,0) (E,7)
G (E,10) (E,7) (E,5) (E,7) (E,3) (E,7) (G,0)
ABCDEFG
------------------------------
A (A,0) (B,3) (B,5) (D,3) (B,7) (B,11) (B,10)
B (A,3) (B,0) (C,2) (C,4) (C,4) (C,8) (C,7)
C (B,5) (B,2) (C,0) (D,2) (E,2) (E,6) (E,5)
D (A,3) (C,4) (C,2) (D,0) (C,4) (C,8) (C,7)
E (C,7) (C,4) (C,2) (C,4) (E,0) (F,4) (G,3)
F (E,11) (E,8) (E,6) (E,8) (E,4) (F,0) (E,7)
G (E,10) (E,7) (E,5) (E,7) (E,3) (E,7) (G,0)
ABCDEFG
------------------------------
A (A,0) (B,3) (B,5) (D,3) (B,7) (B,11) (B,10)
B (A,3) (B,0) (C,2) (C,4) (C,4) (C,8) (C,7)
C (B,5) (B,2) (C,0) (D,2) (E,2) (E,6) (E,5)
D (A,3) (C,4) (C,2) (D,0) (C,4) (C,8) (C,7)
E (C,7) (C,4) (C,2) (C,4) (E,0) (F,4) (G,3)
F (E,11) (E,8) (E,6) (E,8) (E,4) (F,0) (E,7)
G (E,10) (E,7) (E,5) (E,7) (E,3) (E,7) (G,0)
Press any key to continue