Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Data File StructureRSS Feeds

Program to find the shortest path

Posted By: Adelmo Fischer     Category: C Programming     Views: 11915

Program to find the shortest path.

Code for Program to find the shortest path in C Programming

#include <stdio.h>
#include <conio.h>

#define INF 9999

void main( )
{
    int arr[4][4] ;
    int cost[4][4] = {
                        7, 5, 0, 0,
                        7, 0, 0, 2,
                        0, 3, 0, 0,
                        4, 0, 1, 0
                    } ;
    int i, j, k, n = 4 ;

    clrscr( ) ;

    for ( i = 0 ; i < n ; i++ )
    {
        for ( j = 0; j < n ; j++ )
        {
            if ( cost[i][j] == 0 )
                arr[i][j] = INF ;
            else
                arr[i][j] = cost[i][j] ;
        }
    }

    printf ( "Adjacency matrix of cost of edges:\n" ) ;
    for ( i = 0 ; i < n ; i++ )
    {
        for ( j = 0; j < n ; j++ )
            printf ( "%d\t", arr[i][j] ) ;
        printf ( "\n" ) ;
    }

    for ( k = 0 ; k < n ; k++ )
    {
        for ( i = 0 ; i < n ; i++ )
        {
            for ( j = 0 ; j < n ; j++ )
            {
                if ( arr[i][j] > arr[i][k] + arr[k][j] )
                    arr[i][j] = arr[i][k] + arr[k][j];
            }
        }
    }

    printf ( "\nAdjacency matrix of lowest cost between the vertices:\n" ) ;
    for ( i = 0 ; i < n ; i++ )
    {
        for ( j = 0; j < n ; j++ )
            printf ( "%d\t", arr[i][j] ) ;
        printf ( "\n" ) ;
    }

    getch( ) ;
}
  
Share: 


Didn't find what you were looking for? Find more on Program to find the shortest path Or get search suggestion and latest updates.

Adelmo Fischer
Adelmo Fischer author of Program to find the shortest path is from Frankfurt, Germany.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
Drashti Bhatt from India Comment on: Sep 06
solve this Question...????

Implement Huffman Coding taking the following text :
=>
“ There is a beautiful garden near my house.”

View All Comments