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

SQL Injection ,Hacking PHP 4.4 sites in seconds

Today I am going to teach you how to hack a certain type of websites with very least efforts. Websites with PHP  4.4 have a SQL injection vulnerability in them which makes their Admin control panel easily accessible,and in just few steps you will access the admin's account of that website. Remember,this tutorial is applicable on PHP 4.4 machines with Apache running in parallel with them. Also,since I will be hacking REAL websites,I will not be displaying their URL’s or else I will be sued!!!. Also this tutorial is only for educational purpose. Here we go!!! Step 1 – Search for them Yep,make a Google dork to find sites running Apache and PHP 4.4 . Its quite easy.You can do this by searching inurl:adminlogin . Step 2 – Scan them Start by scanning them using Nmap ,Do and intense scan and find the open ports. If you find port 2000 open,then you have almost got it. most websites running PHP4.4 have this port for admin login. Now just login using port 2000 ie - ...

Software Testing through Fuzzing

Fuzz testing or fuzzing is a software testing technique used to discover coding errors and security loopholes in software, operating systems or networks by inputting massive amounts of random data, called fuzz, to the system in an attempt to make it crash . Fuzzing is  often automated or semi-automated, that  involves providing invalid, unexpected, or random data to the inputs of a computer program. The program is then monitored for exceptions such as crashes, or failing built-in code assertions or for finding potential memory leaks. Fuzzing technique is commonly used to test for security problems in software or computer systems ans also used to discover coding errors and security loopholes in software, operating systems  or networks by inputting massive amounts of random data, called fuzz, to the system  in an attempt to make it crash. If a vulnerability is found, a tool called a fuzz tester (or fuzzer), indicates potential causes. There are two forms ...