Skip to main content

Bellmanford Algorithm C++ Program


The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all of the other vertices in a weighted digraph.It is slower than Dijkstra's algorithm for the same problem, but more versatile, as it is capable of handling graphs in which some of the edge weights are negative numbers.





Here is the Source Code:


#include<iostream>
#include<stdio.h>
using namespace std;
#include<conio.h>
#define INFINITY 999
struct node
{
    int cost;
    int value;
    int from;
}a[5];
void addEdge(int am[][5],int src,int dest,int cost)
{
     am[src][dest] = cost;
     return;
}
void bell(int am[][5])
{
    int i, j, k, c = 0, temp;
    a[0].cost = 0;
    a[0].from = 0;
    a[0].value = 0;
    for (i = 1; i < 5; i++)
    {
        a[i].from = 0;
        a[i].cost = INFINITY;
        a[i].value = 0;
    }
    while (c < 5)
    {
        int min = 999;
        for (i = 0; i < 5; i++)
        {
            if (min > a[i].cost && a[i].value == 0)
            {
                min = a[i].cost;
            }
            else
            {
                continue;
            }
        }
        for (i = 0; i < 5; i++)
        {
            if (min == a[i].cost && a[i].value == 0)
            {
                break;
            }
            else
            {
                continue;
            }
        }
        temp = i;
        for (k = 0; k < 5; k++)
        {
            if (am[temp][k] + a[temp].cost < a[k].cost)
            {
                a[k].cost = am[temp][k] + a[temp].cost;
                a[k].from = temp;
            }
            else
            {
                continue;
            }
        }
        a[temp].value = 1;
        c++;
    }
    cout<<"Cost"<<"\t"<<"Source Node"<<endl;
    for (j = 0; j < 5; j++)
    {
        cout<<a[j].cost<<"\t"<<a[j].from<<endl;
    }
}
int main()
{
    int n, am[5][5], c = 0, i, j, cost;
    for (int i = 0; i < 5; i++)
    {
        for (int j = 0; j < 5; j++)
        {
            am[i][j] = INFINITY;
        }
    }
    while (c < 8)
    {
        cout<<"Enter the source, destination and cost of edge\n";
        cin>>i>>j>>cost;
        addEdge(am, i, j, cost);
        c++;
    }
    bell(am);
    getch();
}

Comments

Popular posts from this blog

C++ Program for Circular Doubly Linked List

Today we are gonna tell you the C++ Program to demonstrate circular doubly linked list. The C++ program is successfully compiled and runs on any system.  Here is the code : #include<iostream> #include<cstdio> #include<cstdlib> using namespace std ;   /*  * Node Declaration  */ struct node { int info ; struct node * next ; struct node * prev ; } * start, * last ; int counter = 0 ; /*  * Class Declaration  */ class double_clist { public : node * create_node ( int ) ; void insert_begin ( ) ; void insert_last ( ) ; void insert_pos ( ) ; void delete_pos ( ) ; void search ( ) ; void update ( ) ; void display ( ) ; void reverse ( ) ; void sort ( ) ; double_clist ( ) { start = NULL ; last = NULL ; ...

Android secret codes

Just type these codes as it is in your phone and see the magic!!!! 1. Phone Information, Usage and Battery –  *#*#4636#*#* 2. IMEI Number – *#06# 3. Enter Service Menu On Newer Phones – *#0*# 4. Detailed Camera Information –  *#*#34971539#*#* 5. Backup All Media Files – *#*#273282*255* 663282*#*#* 6. Wireless LAN Test – *#*#232339#*#* 7. Enable Test Mode for Service –*#*#197328640#*#* 8. Back-light Test – *#*#0842#*#* 9. Test the Touchscreen – *#*#2664#*#* 10. Vibration Test – *#*#0842#*#* 11. FTA Software Version – *#*#1111#*#* 12. Complete Software and Hardware Info –*#12580*369# 13. Diagnostic Configuration – *#9090# 14. USB Logging Control – *#872564# 15. System Dump Mode – *#9900# 16. HSDPA/HSUPA Control Menu – *#301279# 17. View Phone Lock Status – *#7465625# 18. Reset the Data Partition to Factory State –*#*#7780#*#* 19. Format Your Device To Factory State(will delete everything on your phone) – *2767*3855# 20. Hidden Servic...

Finding IP address using facebook!!!!!

If you want to check for ip address of particular person on facebook or orkut or any other social site just invite them for a chat, so that your browser should connect to that system, than only when chat is ON open command prompt type the below command Netstat –an This will show you the  connected ip addresses, than from shown ip addresses search for suspicious ip address that is not the local connection address. Local connections normally start from 192.168.1.1 ranging to 192.168.1.255 Other netstat commands: -a Displays all connections and listening ports. -e Displays Ethernet statistics. This may be combined with the -s option. -n Displays addresses and port numbers in numerical form. -p proto Shows connections for the protocol specified by proto; proto may be TCP or UDP. -s option to display per-protocol statistics, proto may be TCP, UDP, or IP. -r Displays the routing table. -s Displays per-protocol statistics. By default, statistics are shown for TCP, UDP and IP; the -p op...