最短路径-迪杰斯特拉算法-单源最短路径

news/2024/7/15 17:30:16 标签: c语言, 图论, c++

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
时间复杂度为 O(n^2)

#include<stdio.h>
#include<stdlib.h>
#define BOOL int
#define TRUE 1
#define FALSE 0
#define T int
#define SIZE 6
#define MAXNUMBER 99
typedef struct graph {
	int NoEdge;
	int Vertices;
	int** A;
}Graph;
void CreateGraph(Graph* g, int n, int noedge) {
	int i, j;
	g->NoEdge = noedge;
	g->Vertices = n;
	g->A = (int**)malloc(n * sizeof(int*));
	for (i = 0; i < n; i++) {
		g->A[i] = (int*)malloc(n * sizeof(int));
		for (j = 0; j < n; j++) {
			g->A[i][j] = noedge;
		}
		g->A[i][i] = 0;
	}
}
void Add(Graph* g, int u, int v, int w) {
	if (u < 0 || v < 0 || u > g->Vertices - 1 || v>g->Vertices - 1 || u == v || g->A[u][v] != g->NoEdge) {
		printf("BadInput\n");
	}
	else {
		g->A[u][v] = w;
	}
}
int ChooseMin(T d[], int n, BOOL s[], T MaxNumber) {
	int i, minpos ;
	T min=MaxNumber;
	for ( i = 0; i < n; i++)
	{
		if (d[i]<=min&& !s[i])
		{
			min = d[i];
			minpos = i;
		}
	}
	return minpos;
}
void Dijkstra(Graph *g, int v ) {
	int i, u, w, n = g->Vertices;
	T d[SIZE] = { 0 };
	T path[SIZE] = { 0 };
	BOOL s[SIZE] = {FALSE};
	if (v<0|| v> n-1)
	{
		printf("输错了\n");
		return;
	}
	for ( i = 0; i < n; i++)
	{
		s[i] = FALSE;
		d[i] = g->A[v][i];
		if (i!=v && d[i]<g->NoEdge)
		{
			path[i] = v;
		}
		else {
			path[i] = -1;
		}
	}
	s[v] = TRUE; d[v] = 0;
	for ( i = 1; i <= n-1; i++)
	{
		u = ChooseMin(d, n, s, g->NoEdge);
		s[u] = TRUE;
		for (w = 0; w < n; w++)
		{
			if (!s[w] && d[u]+g->A[u][w]<d[w])
			{
				d[w] = d[u] + g->A[u][w];
				path[w] = u;
			}
		}
	}
	for ( i = 0; i < n; i++)
	{
		printf("%d ", s[i]);
	}
	printf("\n");
	for (i = 0; i < n; i++)
	{
		printf("%d ", d[i]);
	}
	printf("\n");
	for (i = 0; i < n; i++)
	{
		printf("%d ", path[i]);
	}
	printf("\n");
}
void Delete(Graph* g, int u, int v) {
	if (u < 0 || v < 0 || u > g->Vertices - 1 || v>g->Vertices - 1 || u == v || g->A[u][v] == g->NoEdge) {
		printf("BadInput\n");
	}
	else {
		g->A[u][v] = g->NoEdge;
		printf("Delete Successfully\n");
	}
}
void Exist(Graph* g, int u, int v) {
	if (u < 0 || v < 0 || u > g->Vertices - 1 || v>g->Vertices - 1 || u == v || g->A[u][v] == g->NoEdge) {
		printf("No Existance\n");
	}
	else {
		printf("Element Exists!\n");
	}
}
void PrintMatrix(Graph* g) {
	int i, j;
	for (i = 0; i < g->Vertices; i++) {
		for (j = 0; j < g->Vertices; j++) {
			printf("%-4d", g->A[i][j]);
		}
		printf("\n");
	}
	printf("***************\n");
}
void main() {
	Graph* g;
	g = (Graph*)malloc(sizeof(Graph));
	CreateGraph(g, SIZE, MAXNUMBER); //在这里网的noedge统一为99,可以看出这是创建了一个网
	PrintMatrix(g);
	Add(g, 0, 1, 50);
	Add(g, 0, 2, 10);
	Add(g, 0, 4, 70);
	Add(g, 1, 2, 15);
	Add(g, 1, 4, 10);
	Add(g, 2, 0, 20);
	Add(g, 2, 3, 15);
	Add(g, 3, 1, 20);
	Add(g, 3, 4, 35);
	Add(g, 4, 3, 30);
	Add(g, 5, 3, 3);
	PrintMatrix(g);
	Dijkstra(g, 0);
}

0 99 99 99 99 99
99 0 99 99 99 99
99 99 0 99 99 99
99 99 99 0 99 99
99 99 99 99 0 99
99 99 99 99 99 0


0 50 10 99 70 99
99 0 15 99 10 99
20 99 0 15 99 99
99 20 99 0 35 99
99 99 99 30 0 99
99 99 99 3 99 0


1 1 1 1 1 1
0 45 10 25 55 99
-1 3 0 2 1 -1


http://www.niftyadmin.cn/n/1365094.html

相关文章

C++的虚函数与多态

C中的虚函数的作用主要是实现了多态的机制。关于多态&#xff0c;简而言之就是用父类型别的指针指向其子类的实例&#xff0c;然后通过父类的指针调用实际子类的成员函数。这种技术可以让父类的指针有“多种形态”&#xff0c;这是一种泛型技术。所谓泛型技术&#xff0c;说白了…

最短路径-弗洛伊德算法-所有顶点之间的最短路径

#include<stdio.h> #include<stdlib.h> #define BOOL int #define TRUE 1 #define FALSE 0 #define T int #define SIZE 4 //指示途中结点数量 #define MAXNUMBER 99 typedef struct graph {int NoEdge;int Vertices;int** A; }Graph; void CreateGraph(Graph* g…

CSS 架构

存档&#xff0c;已备学习。原文路径&#xff1a;http://www.admin10000.com/document/1229.html 英文原文&#xff1a;http://engineering.appfolio.com/2012/11/16/css-architecture/ Philip Walton 在AppFolio担任前端工程师&#xff0c;他在Santa Barbara on Rails的聚会上…

邻接表有向图是否包含回路

//我在这里其实是找不到有几条回路的&#xff0c;这个函数只能判断图里面有没有回路。下面的那个找出几条回路的是不完善的。如果你能完善这些功能&#xff0c;请在评论区给出你的答案&#xff0c;谢谢 #include<stdio.h> #include<stdlib.h> //int target0; //相…

SQL Server replication requires the actual server name to make a connection to the server.错误解决...

今天&#xff0c;在作数据发库时&#xff0c;出现如下错误&#xff1a; "SQL Server replication requires the actual server name to make a connection to the server. Connections through a server alias, IP address, or any other alternate name are not supporte…

直接插入排序-链表形式与顺序表形式

//链表形式的直接插入排序 #include<stdio.h> #include<stdlib.h> typedef int T; typedef struct node {T element;struct node* Link; }Node; typedef struct list {Node* First; }List; Node* NewNode(T x) {Node* n (Node*)malloc(sizeof(Node));n->elemen…

c#中在规定时间弹出窗体

我现在做了一个幼儿教育软件~我想在规定时间提醒他关闭程序 该休息休息 了&#xff01;如果他还不管3分钟内强制关闭~&#xff1f;请问这个该怎么弄谢谢你们&#xff1f; 问题补充&#xff1a; 我希望能给出来完整的代码 我是初学 &#xff0c;&#xff0c;是winform程序~~ 在 …

微软企业库调用Oracle存储过程返回(1个或多个)数据集(转载)

以前用企业库读SQL Server返回数据集没任何问题&#xff0c;可以返回1个也可以返回多个&#xff0c;读Oracle的时候返回一个数据集的时候也没问题&#xff0c;可是最近在用Oracle返回多个数据集的时候却出了问题&#xff0c;几经辗转&#xff0c;终于找到了解决方案&#xff0c…