首页 > 其他分享 >c: Dijkstra's Algorithm

c: Dijkstra's Algorithm

时间:2023-09-26 17:12:10浏览次数:36  
标签:distance Algorithm int Graph start Dijkstra MAX

 

DijkstrasAlgorithm.h  
/**
 * *****************************************************************************
 * @file        DijkstrasAlgorithm.h
 * @brief       Dijkstra's Algorithm in C 迪杰斯特拉算法 最短路径算法 https://www.programiz.com/dsa/dijkstra-algorithm#google_vignette
 * IDE: VSCODE C11
 * @author       (geovindu,Geovin Du,涂聚文)
 * @date        2023-09-26
 * @copyright   geovindu
 * *****************************************************************************
 */

#ifndef DIJKSTRASALGORITHM_H
#define DIJKSTRASALGORITHM_H

#include <stdio.h>

#define INFINITY 9999
#define MAX 10

/**
 * @brief     Dijkstra's Algorithm  
 * 
 * @param       Graph 
 * @param       n 
 * @param       start 
 */
void Dijkstra(int Graph[MAX][MAX], int n, int start);


#endif

  

DijkstrasAlgorithm.c  
/**
 * *****************************************************************************
 * @file        DijkstrasAlgorithm.c
 * @brief    Dijkstra's Algorithm in C 迪杰斯特拉算法 最短路径算法
 * IDE: VSCODE C11    https://www.programiz.com/dsa/dijkstra-algorithm#google_vignette
 * @author       (geovindu,Geovin Du,涂聚文)
 * @date        2023-09-26
 * @copyright   geovindu
 * *****************************************************************************
 */

#include <stdio.h>
#include "include/DijkstrasAlgorithm.h"



/**
 * @brief       
 * 
 * @param       Graph 
 * @param       n 
 * @param       start 
 */
void Dijkstra(int Graph[MAX][MAX], int n, int start) {
  int cost[MAX][MAX], distance[MAX], pred[MAX];
  int visited[MAX], count, mindistance, nextnode, i, j;

  // Creating cost matrix
  for (i = 0; i < n; i++)
    for (j = 0; j < n; j++)
      if (Graph[i][j] == 0)
        cost[i][j] = INFINITY;
      else
        cost[i][j] = Graph[i][j];

  for (i = 0; i < n; i++) {
    distance[i] = cost[start][i];
    pred[i] = start;
    visited[i] = 0;
  }

  distance[start] = 0;
  visited[start] = 1;
  count = 1;

  while (count < n - 1) {
    mindistance = INFINITY;

    for (i = 0; i < n; i++)
      if (distance[i] < mindistance && !visited[i]) {
        mindistance = distance[i];
        nextnode = i;
      }

    visited[nextnode] = 1;
    for (i = 0; i < n; i++)
      if (!visited[i])
        if (mindistance + cost[nextnode][i] < distance[i]) {
          distance[i] = mindistance + cost[nextnode][i];
          pred[i] = nextnode;
        }
    count++;
  }

  // Printing the distance
  for (i = 0; i < n; i++)
    if (i != start) {
      printf("\nDijkstra's Algorithm Distance from source to %d: %d", i, distance[i]);
    }
}

  

调用:

   //15 Dijkstra's Algorithm 迪杰斯特拉算法 最短路径算法
    int Graph[MAX][MAX], j, u;
    n = 7;

    Graph[0][0] = 0;
    Graph[0][1] = 0;
    Graph[0][2] = 1;
    Graph[0][3] = 2;
    Graph[0][4] = 0;
    Graph[0][5] = 0;
    Graph[0][6] = 0;

    Graph[1][0] = 0;
    Graph[1][1] = 0;
    Graph[1][2] = 2;
    Graph[1][3] = 0;
    Graph[1][4] = 0;
    Graph[1][5] = 3;
    Graph[1][6] = 0;

    Graph[2][0] = 1;
    Graph[2][1] = 2;
    Graph[2][2] = 0;
    Graph[2][3] = 1;
    Graph[2][4] = 3;
    Graph[2][5] = 0;
    Graph[2][6] = 0;

    Graph[3][0] = 2;
    Graph[3][1] = 0;
    Graph[3][2] = 1;
    Graph[3][3] = 0;
    Graph[3][4] = 0;
    Graph[3][5] = 0;
    Graph[3][6] = 1;

    Graph[4][0] = 0;
    Graph[4][1] = 0;
    Graph[4][2] = 3;
    Graph[4][3] = 0;
    Graph[4][4] = 0;
    Graph[4][5] = 2;
    Graph[4][6] = 0;

    Graph[5][0] = 0;
    Graph[5][1] = 3;
    Graph[5][2] = 0;
    Graph[5][3] = 0;
    Graph[5][4] = 2;
    Graph[5][5] = 0;
    Graph[5][6] = 1;

    Graph[6][0] = 0;
    Graph[6][1] = 0;
    Graph[6][2] = 0;
    Graph[6][3] = 1;
    Graph[6][4] = 0;
    Graph[6][5] = 1;
    Graph[6][6] = 0;

    u = 0;
    Dijkstra(Graph, n, u);

  

标签:distance,Algorithm,int,Graph,start,Dijkstra,MAX
From: https://www.cnblogs.com/geovindu/p/17730662.html

相关文章

  • c: Ford - Fulkerson Algorithm
     FordFulkersonAlgorithm.h /*********************************************************************************@fileFordFulkersonAlgorithm.h*@briefFord-FulkersonAlgorithminCFord-Fulkerson算法(FFA)是一种贪婪算法,用于计算流网络......
  • c: Kruskal Algorithm
    KruskalAlgorithm.h /*****************************************************************//***\fileKruskalAlgorithm.h*\briefKruskalAlgorithm克鲁斯卡尔算法*IDE:VSCODEc11https://github.com/hustcc/JS-Sorting-Algorithm/blob/master/2.selectio......
  • python: Sorting Algorithms
     #encoding:utf-8#版权所有2023涂聚文有限公司#许可信息查看:PythonSortingAlgorithms#描述:*https://www.programiz.com/dsa/counting-sort#*https://www.geeksforgeeks.org/sorting-algorithms/#Author:geovindu,GeovinDu涂聚文.#IDE:PyC......
  • python: Essential Algorithms
     #encoding:utf-8#版权所有2023涂聚文有限公司#许可信息查看:#描述:#Author:geovindu,GeovinDu涂聚文.#IDE:PyCharm2023.1python311#Datetime:2023/9/2121:28#User:geovindu#Product:PyCharm#Project:EssentialAlgor......
  • c: Sorting Algorithms
    SortAlgorithm.h /*****************************************************************//***\fileSortAlgorithm.h*\brief业务操作方法*VSCODEc11https://github.com/hustcc/JS-Sorting-Algorithm/blob/master/2.selectionSort.md*https://www.progra......
  • 论文阅读: Co-design Hardware and Algorithm for Vector Search
    1.Introduction介绍一下论文背景,向量检索常用于搜索引擎,推荐系统,LLM和科学计算等对应的常用的硬件向量检索方法,IVF-PQ其中IVF:将多个向量聚类,PQ将向量压缩而为了最大化IVF-PQ的效果,也会面临很多的挑战在芯片设计的过程中,会遇到针对六个阶段如何设计合适的微架构?如何将有......
  • dijkstra 模板
    Input输入数据有多组,每组的第一行是三个整数T,S和D,表示有T条路,和草儿家相邻的城市的有S个,草儿想去的地方有D个;接着有T行,每行有三个整数a,b,time,表示a,b城市之间的车程是time分钟;(1=<(a,b)<=1000;a,b之间可能有多条路)接着的第T+1行有S个数,表示和草儿家相连的城市;接着的第T+2行......
  • Proj. CRR Paper Reading: Optimal Speedup of Las Vegas Algorithms, Adaptive resta
    TitleAdaptiverestartforstochasticsynthesisPLDI2021TaskDistributethepowerbetweenmultiplerunsinstochasticprogramsynthesistoaccelerateHere,astochasticprogramsynthesisprogramcanbesummarizedasfollows:Givenasetof<input,ou......
  • 解决npm ERR! Cannot read properties of null (reading ‘pickAlgorithm‘)报错问题
    转载自:https://www.cnblogs.com/zhyp/p/16920380.html=========解决方法:在终端中运行命令:npmcacheclear--force然后重新运行npmi命令,再次安装安装完成,没有出现报错npmrunserve运行项目,项目可以正常启动了。  安装vueCLI失败后,百度得知在终端执行命令:npmcleanc......
  • Data structure and algorithm-Two
    B树                扩容           找出不含重复字符的最长字串的长度 字母异位词分组   优化用一个长度26的整数数组来标识 ArrayKey的构造方法   判断是否存在重复元素 ......