我想创建一个表示无向图的邻接矩阵(以实现Dijkstra算法)。我通过创建填充了随机数的N * N矩阵来启动我的代码。但是,它希望使图形不完全连接,因此矩阵必须包含INFTY,表示任何节点对之间没有路径。因此,我如何随机在矩阵生成过程中添加INFNTY值,如下所示:
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <math.h>
#define INFTY 99 // Define Infinity as a macro
int main (int argc, char* argv[]) {
/* Local Variables */
int N = 40; // Number of Nodes
int SOURCE = 0; // Selected Source
int i,j;
/* Matrix Allocation for edges */
int *edge[N];
for (i = 0; i < N; i++){
edge[i] = malloc(N * sizeof(int));
}
/* Randomely fill the matrix with random integers from 0-10 */
srand(0);
for (i = 0; i < N; i++){
for (j = 0; j < N; j++){
if(i == j)
edge[i][j] = 0;
else
edge[i][j] = rand() % 10; // Can I do something here to insert INFTY randomly.
}
}
}
答案 0 :(得分:1)
这是你能做的:
for (i = 0; i < N; i++){
for (j = 0; j < N; j++){
if(i == j)
edge[i][j] = 0;
else {
int r = rand() % 11
edge[i][j] = r == 10 ? INFTY : r;
}
}
这意味着大约每第11个边缘都是INF
。如果你想增加矩阵的稀疏度,你可以:
int sparsity_rate = 50; // measured in %
...
if (rand() % 1011 <= sparsity_rate)
edge[i][j] = INFTY;
else
edge[i][j] = rand() % 10;
其中sparsity_rate
告诉您图表的备用量(以%为单位)。在上面的例子中,大约50%的边缘将消失。
答案 1 :(得分:0)
这是Dijkstra算法的一种实现。
基本实施取自:http://code.geeksforgeeks.org/index.php
注意:没有malloc,没有免费,没有深奥的代码。
由#define V (9)
控制的图表大小,因此很容易改变大小
此算法适用于您可能希望展开以使用多个来源的单个source
。
// A C program for Dijkstra's single source shortest path algorithm.
// The program is for adjacency matrix representation of the graph
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <stdbool.h>
#include <time.h>
// Number of vertices in the graph
#define V (9)
// A utility function to find the vertex with minimum distance value, from
// the set of vertices not yet included in shortest path tree
int minDistance(int dist[], bool sptSet[])
{
// Initialize min value
int min = INT_MAX, min_index;
int v;
for (v = 0; v < V; v++)
if (sptSet[v] == false && dist[v] <= min)
min = dist[v], min_index = v;
return min_index;
}
// A utility function to print the constructed distance array
void printSolution(int dist[], int n)
{
printf("Vertex Distance from Source\n");
int i;
for (i = 0; i < n; i++)
{
printf("%d \t\t %d\n", i, dist[i]);
}
}
// Function that implements Dijkstra's single source shortest path algorithm
// for a graph represented using adjacency matrix representation
void dijkstra(int graph[V][V], int src)
{
int dist[V]; // The output array. dist[i] will hold the shortest
// distance from src to i
bool sptSet[V]; // sptSet[i] will true if vertex i is included in shortest
// path tree or shortest distance from src to i is finalized
// Initialize all distances as INFINITE and stpSet[] as false
int i;
for (i = 0; i < V; i++)
dist[i] = INT_MAX, sptSet[i] = false;
// Distance of source vertex from itself is always 0
dist[src] = 0;
// Find shortest path for all vertices
int count;
for (count = 0; count < V-1; count++)
{
// Pick the minimum distance vertex from the set of vertices not
// yet processed. u is always equal to src in first iteration.
int u = minDistance(dist, sptSet);
// Mark the picked vertex as processed
sptSet[u] = true;
// Update dist value of the adjacent vertices of the picked vertex.
int v;
for (v = 0; v < V; v++)
// Update dist[v] only if is not in sptSet, there is an edge from
// u to v, and total weight of path from src to v through u is
// smaller than current value of dist[v]
if ( !sptSet[v]
&& graph[u][v]
&& dist[u] != INT_MAX
&& dist[u]+graph[u][v] < dist[v]
)
{
dist[v] = dist[u] + graph[u][v];
}
}
// print the constructed distance array
printSolution(dist, V);
}
// driver program to test above function
int main( void )
{
int graph[V][V];
srand( time(NULL) );
int i; // loop counter
int j; // loop counter
for( i=0; i<V; i++) // row loop
{
for( j=0; j<V; j++) // column loop
{
graph[i][j] = rand() % V; // yields values in range 0..(V-1)
}
}
dijkstra(graph, 0);
return 0;
}