LSP算法的输入文件格式

时间:2014-11-11 04:49:43

标签: algorithm linked-list dijkstra

亲爱的所有可以帮助的人,

我(至少我认为我有)编写了一个用于实现简单链接状态算法的代码,是的,代码的灵感来自于我在网上看到的某人的工作。所以我决定保持这种方式。当我的代码运行时,它要求输入文件来构建有向图和Adjacency Matrix。现在问题是我尝试了两种类型的输入文件,输出似乎没有变化。

你能帮忙吗?

我的输入文件如下所示: -

第一种类型

1 2 4

1 4 6

2 3 5

2 1 2

第二种类型

A B 4

A D 6

B C 5

B A 2

我的代码如下: -

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#define MAX 26
#define UNVISITED -1
#define VISITED 1
#define INFINITY 32767
int adjMat[MAX][MAX],n;
void view();
int search(int src,int des,int pathmat[MAX],int *minLen); //Implementation of Searching in  Linked List//
typedef struct
{
int previous,len,status;
}node;
int n;
void main()
{

    char ch,s,d;
    int i,j,k,src,des,minLen,tot,pathMat[MAX];
    FILE *f1;
    printf("Enter the number of vertices for the Weighted Graph: ");
    scanf("%d", &n);

    //n=4;

    printf("\nThe number of vertices of Weighted Graph: %d\n",n);
    f1 = fopen("input.txt","r");  //Reading an input file for determining the weights of the paths for the graph

    if((f1=fopen("input.txt","r"))==NULL)
    {
        fprintf(stderr,"Cannot open input file.");
        return;
    }
    char r;
    //fscanf(f1,"%s",&adjMat[i][j]);
    //fscanf(f1,"%c",r);
    //printf("f1: %s \n",r);
    //printf();
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++){
                fscanf(f1,"%c",&adjMat[i][j]);
        }
    }

    fclose(f1);
    printf(" The adjacency matrix is . . \n");
    char q;
    for (q=65; q<=n+64; q++)
    printf("         %c", q);  //Implementation of a Dynamic label printing based on the number of nodes entered in the start
    printf("\n");
    view();
    while(1)
    {
        printf("\nEnter the Source Node:");  //Implementation of Single Source Static Shortest Path Dijkstra
        fflush(stdin);
        scanf("%c",&s);
        src=toupper(s)-64;

        printf("\nDES    COST    NEXT\n");
        for(i=1;i<=n;i++)
            {
                tot=search(src,i,pathMat,&minLen);
                printf("\n%c     %d",i+64,minLen);
                if(src==i)
                printf("       -");
                else
                printf("       %c",pathMat[tot-1]+64);
                printf("\n");
            }

        printf(" Do you want to change the source node (Y/N):");
        ch=getche();
        if(ch!='Y'&&ch!='y')
        break;
    }
}
void view()
{
    int i,j;
    printf("");
    for(i=1;i<=n;i++)
    {
    //printf("%4c",i+64);
    //for(i=1;i<=n;i++)
    {
        printf("");
        for(j=1;j<=n;j++)
        {
            if(j==1)
            printf("\n %c",i+64);
            printf("    %d",adjMat[i][j]);
        }
               printf("\n");
    }
    }
}// Below is the implementation of the function to read the input file traverse the linked list and
//build the adjacency matrix based on the weights given in the input file
int search(int src,int des,int pathMat[MAX],int *minLen)
{
    node graph[MAX];
    int i,k,min,tot=0,Vertex,newLen,u,v;
    *minLen=0;

    for(i=1;i<=n;i++)
    {
        graph[i].previous=0;
        graph[i].len=INFINITY;
        graph[i].status=UNVISITED;
    }
    graph[src].previous=0;
    graph[src].len=0;
    graph[src].status=VISITED;
    Vertex=src;
    while(Vertex!=des)
    {
        for(k=1;k<=n;k++)
        {
            if(adjMat[Vertex][k]>0&&graph[k].status==UNVISITED)
            {
                    newLen=graph[Vertex].len+adjMat[Vertex][k];
                    if(newLen<graph[k].len)
                {
                    graph[k].previous=Vertex;
                    graph[k].len=newLen;
                }
            }
        }
        min=INFINITY;
        Vertex=0;
        for(i=1;i<=n;i++)
        if(graph[i].status==UNVISITED&&graph[i].len<min)
        {
            min=graph[i].len;
            Vertex=i;
        }

        if(Vertex==0)
        return 0;
        graph[Vertex].status=VISITED;
    }
    while(Vertex!=0)
    {
        pathMat[++tot]=Vertex;
        Vertex=graph[Vertex].previous;
    }
    for(i=tot;i>1;i--)
    {
        u=pathMat[i];
        v=pathMat[i-1];
        *minLen=*minLen+adjMat[u][v];
    }

    return(tot);
}

0 个答案:

没有答案