Adjacency list and struct Dijkstra - c

I must create a Dijkstra algorithm program using an adjacency list. My teacher give me this struct.
But I have this error :
note: expected 'struct maillon **' but argument is of type 'LISTE'
void insere(int som_a, int som_b, int poids, LISTE Adj[]){
Where the argument nom is vertices, poids is weight, som_a and som_b are vertices.
function void insere : insert (som_b, poids) At the top of the adjacency list Adj[som_a]
typedef struct maillon{
struct maillon *suiv;
int nom;
int poids;
} MAILLON, *LISTE;
void insere(int som_a, int som_b, int poids, LISTE Adj[]) {
LISTE prem = malloc(sizeof(LISTE));
prem->nom = som_b;
prem->poids = poids;
prem->suiv = Adj[som_a];
Adj[som_a] = prem;
}
void dijsktra(int s, GRAPHE G) {
int i, j, dist[NB_SOM_MAX], INT_MAX = 0, pred[NB_SOM_MAX], min, nb = 0, nbmin = 0;
LISTE S,F = G.Adj;
for(i = 0; i < G.nbSommets; i++) {
dist[i] = INT_MAX;
pred[i] = NULL;
}
dist[0] = 0;
S = NULL;
while(F != NULL){
min = G.Adj[0]->poids;
for(i = 1; i < G.nbSommets; i++) {
if(min > G.Adj[i]->poids) {
min = G.Adj[i]->poids;
nbmin = i;
}
}
insere(nb, nbmin, min, S);
nb++;
if(nbmin == 0){
F = F->suiv;
}
else{ // F[nbmin-1]->suiv = F[nbmin + 1];
F[nbmin - 1] = F[nbmin + 1];
}
for(i = G.Adj[nbmin]->nom; i < G.nbSommets; i++){
if(dist[i] > dist[nbmin] + G.Adj[nbmin]->poids){
dist[i] = dist[nbmin] + G.Adj[nbmin]->poids;
pred[i] = nbmin;
}
}
}
for(i = 0; i < G.nbSommets; i++){
printf("Chemin optimal de %d à %d : ", i, s);
printf("%d-", i);
j = i;
while(pred[j] != s || pred[j] != NULL){
printf("%d-", pred[j]);
j = pred[j];
}
printf("\n");
}
}

Related

How to store strings in a 2D array in C?

I want to store strings in a char * array but I don't know how I can do that.
Each cell in the 2d array can contain letters or numbers with 2 digits.
|_|S|_|
|_|10|_|
|_|W|_|
|_|_|_|
|_|_|_|
I tried this, I used struct:
struct Etage {
char Idsalles[20];
int** etage;
int width;
int height;
};
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include "etage.h"
#define COLS 15
#define ROWS 9
Etage *createMap()
{
Etage *e = malloc(sizeof(Etage));
e->width = COLS; // columns
e->height = ROWS; // rows
e->etage = malloc(sizeof(char *) * e->height);
for (int i = 0; i < e->height; i += 1)
{
e->etage[i] = malloc(sizeof(char) * e->width);
for (int j = 0; j < e->width; j += 1)
{
e->etage[i][j] = "0";
}
}
return e;
}
void randomId(Etage *e, int maxSalles)
{
srand(time(NULL));
int i = 0;
if (maxSalles < 10)
{
printf("Seulement %d disponibles, veuiller générer plus de salles.\n", maxSalles);
return;
}
while (i < 10)
{
int id = (rand() % maxSalles) + 1;
int existe = testId(e, id);
if (existe != 1)
{
e->Idsalles[i] = id;
i += 1;
}
}
return;
}
int testId(Etage *e, int id)
{
for (int i = 0; i < 10; i += 1)
{
if (id == e->Idsalles[i])
{
return 1;
}
}
return 0;
}
void printEtage(Etage *e)
{
for (int i = 0; i < e->height; i += 1)
{
for (int j = 0; j < e->width; j += 1)
{
if(e->etage[i][j] == 0){
printf(" ");
}else{
printf("%s", e->etage[i][j]);
printf(" ");
}
}
printf("\n");
}
printf("Id des salles de cette étage: ");
for(int i =0; i <10;i+=1){
printf("%d ",e->Idsalles[i]);
}
printf("\n");
}
void freeEtage(Etage *e)
{
//free(e->etage);
free(e);
}
void placerSalles(Etage* e){
int a=ROWS/2;
int b=COLS/2;
//0 = Haut; 1 = Droite; 2 = Bas; 3 = Gauche
e->etage[a][b] = e->Idsalles[0]+"\0"; // On place la premiere salle au centre de l'étage sa sera le spawner 'S'
srand(time(NULL));
int i = 1;
while(i<10){
int dir = randomDirections();
if(dir==0){ // On se déplace en haut
a = a-1; //On se déplace
if(e->etage[a][b] == 0 && a > 0){
e->etage[a][b] = e->Idsalles[i]+"\0";
i+=1;
}else{
a = a+1; // Sionon on revien a la derniere case connu
}
}else if(dir==1){ // On se déplace à droite
b=b+1;
if(e->etage[a][b] == 0 && b< e->width){
e->etage[a][b] = e->Idsalles[i]+"\0";
i+=1;
}else{
b = b-1;
}
}else if(dir==2){ // On se déplace en bas
a = a+1;
if(e->etage[a][b] == 0 && a>e->height){
e->etage[a][b] = e->Idsalles[i]+"\0";
i+=1;
}else{
a = a-1;
}
}else if(dir==3){ // On se déplace à gauche
b = b-1;
if(e->etage[a][b] == 0 && b>0){
e->etage[a][b] = e->Idsalles[i]+"\0";
i+=1;
}else{
b = b+1;
}
}
}
}
int randomDirections(){
int i = 0;
int id = rand() % 4; // Remplacé 10 par le nbrSalles
//printf("Position: %d\n",id);
return id;
}
I have a good understanding with 2d array but three...
I tried using malloc.
I don't even think this is possible...
If your string is a maximum of 2 characters long (your strings will be 3 chars long)
char array[rows][cols][3] = {{"2", "a", "34"}, ... };
or to use malloc
char (*array)[cols][3] = malloc(rows * sizeof(*array));

I am trying to solve a special travelling salesman problem with mpi and c

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <mpi.h>
#define MaxSize 50
typedef int ElementType;
typedef int Position;
typedef int Status;
typedef struct SeqStack
{
ElementType Data[MaxSize];
Position Top;
int (*Push)(struct SeqStack *L);
int (*Pop)(struct SeqStack *L , int e);
int (*isEmpty)(struct SeqStack s);
Status (*isFull)(struct SeqStack s);
}SeqStack;
int Push(SeqStack *L)
{
if(L->Top == 0)
{
return 0;
}
printf("%d ",L->Data[--L->Top]);
return 1;
}
int Pop(SeqStack *L , int e)
{
if(L->Top==MaxSize -1)
{
return 0;
}
L->Data[L->Top++] = e;
return 1;
}
int isEmpty(SeqStack s)
{
if(s.Top != 0)
{
return 1;
}
return 0;
}
Status isFull(SeqStack s)
{
if(s.Top != MaxSize -1)
{
return 1;
}
return 0;
}
int global_max;
int ans = INT_MAX;
void *tsp(int Dis[global_max][global_max],int v[],int N,int count,int currPos,int cost,int sum,int* result,int temp[],int make_log){
temp[0]=1;
//int mode;
//printf("count:%d\n",count);
if(count == N&&Dis[currPos][1]>0){
//printf("Ans:%d,cost:%d\n",ans,cost);
if(make_log == 1){
char str[100]={'\0'};
char output[100]={'\0'};
//printf("Ans:%d,cost:%d\n",ans,cost);
FILE *log;
for(int i=0; i<N;i++){
if (i!=N-1){
sprintf(str,"%d",temp[i]);
strcat(output,str);
strcat(output,",");
}else{
sprintf(str,"%d",temp[i]);
strcat(output,str);
strcat(output,"=");
}
}
sprintf(str,"%d",cost);
strcat(output,str);
strcat(output,"\n");
log=fopen("log.txt","a");
fputs(output,log);
fclose(log);
}
if(sum> cost){
sum = cost;
ans = cost;
//printf("every ans is %d\n\n\n\n",ans);
for (int i=0;i<N;i++){
//printf("temp[%d]=%d,",i,temp[i]);
result[i]=temp[i];
//printf("result[%d]=%d,",i,result[i]);
}
}
//ans = min(ans,cost + Dis[1][currPos]);
return result;
}
for (int i = 1;i<N+1;i++){
//printf("!!!!!!! v[%d] = %d\n",i,v[i]);
if(v[i]==0&&Dis[currPos][i]>0){
//printf("cost + Dis: %d + %d\n",cost,Dis[currPos][i]);
if(cost + Dis[currPos][i] <= ans||count==N-1){
v[i] = 1;
temp[count] = i;
//printf("\ntemp[%d] = %d\n",count,temp[count]);
//printf("currPos:%d,i:%d,count:%d\n",currPos,i,count);
//printf("Ans:%d,cost:%d\n",ans,cost);
result = tsp(Dis,v,N,count + 1,i,cost + Dis[currPos][i],sum,result,temp,make_log);
//mode = 0;
v[i]= 0;
if(count==1){
for(int j = 2;j<N+1;j++){
v[j]= 0;
}
}
}else{
//printf("currPos:%d,i:%d,count:%d\n",currPos,i,count);
temp[count] = i;
for (int k = N-1;k>count;k--){
temp[k] = 0;
}
int v_copy[N];
for (int o = 1;o<N+1;o++){
v_copy[o] = v[o];
// if(currPos==3&&i==4){
// printf("v_copy[%d] = %d//",o,v_copy[o]);
// }
}
for(int j = 2;j< N + 1;j++){
if(v[j]==0){
v[j] = 1;
}
}
result = tsp(Dis,v,N,N,N,cost + Dis[currPos][i],sum,result,temp,make_log);
//printf("Fuck currpos:%d\n",currPos);
for (int o = 1;o<N+1;o++){
v[o] = v_copy[o];
// if(currPos==3&&i==2){
// printf("v[%d] = %d//",o,v[o]);
// }
}
if(count==1){
for(int j = 2;j<N+1;j++){
v[j]= 0;
}
}
}
}
}
return result;
};
void change(int n){
global_max = n;
};
int main(int argc, char const *argv[])
{
int N;
int size = 1;
int count = 1;
int log = 0;
int nthreads,my_rank;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &nthreads);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
if(argc > 2){
log = 1;
}
char x[100] = {0};
char filename[100] = {};
strcat(filename,argv[1]);
strcat(filename,".txt");
//printf("%s\n",filename);
FILE *fp=fopen(filename,"r");
if(fp==NULL){
printf("Cannot open the file,strike any key to exit!\n");
getchar();
exit(0);
}
for (int i = 0;!feof(fp);i++){
fscanf(fp,"%hhd",&x[i]);
}
N=x[0];
change(N);
int* result;
result = (int *)malloc(sizeof(int) * global_max);
int Dis[N][N], City[N];
for(int i=1;i<N;i++){
size=size*i;
}
char y[size];
for (int i=1;i<size+1;i++){
y[i] = x[i];
//printf("%d\n",y[i]);
}
for(int i=2; i < N + 1; i++){
for(int j=1; j < i ; j++){
Dis[j][i] = y[count];
Dis[i][j] = y[count];
count+=1;
//printf("(%d,%d),(%d,%d)",j,i,i,j);
//printf("%d\n",Dis[j][i]);
}
}
for(int i=0;i<N;i++){
City[i]=i + 1;//create the number of city with 1 ...... N
}
int curr_constraint = 0;
int v[N+1];
for (int i = 1; i < N+1; i++){
v[i] = 0;
}
for(int i = 1;i<N+1;i++){
curr_constraint += Dis[i][i+1];
}
v[1]= 1;
int sum = INT_MAX;
//printf("orginal ans is %d\n",ans);
//printf("Dis map:\n");
for( int i= 1;i<N+1;i++){
for(int j =1;j<N+1;j++){
if(i==j){
Dis[i][j]=0;
}
//printf("%d ",Dis[i][j]);
}
//printf("\n");
}
//printf("The orginal constraint is %d\n",curr_constraint);
int temp[N];
int* city_divided;
int cityNumber;
int current_ans;
SeqStack s;
if(nthreads > 1){
int remain = N % (nthreads - 1);
//int group_n = N /(nthreads - 1);
if(remain !=0){
if(my_rank <= remain && my_rank != 0){
cityNumber = ((N - remain)/(nthreads - 1)) + 1;
city_divided = (int*)malloc(cityNumber*sizeof(int));
for (int i = 0; i <cityNumber; i++)
{
city_divided[i] = City[(my_rank-1)*cityNumber + 1];
}
}else{
cityNumber = ((N - remain) / (nthreads - 1));
city_divided = (int*)malloc(cityNumber*sizeof(int));
for (int i = 0; i <cityNumber; i++)
{
city_divided[i] = City[(my_rank-1)*cityNumber + 1];
}
}
}else{
cityNumber = N / (nthreads - 1);
city_divided = (int*)malloc(cityNumber*sizeof(int));
for (int i = 0; i <cityNumber; i++)
{
city_divided[i] = City[(my_rank-1)*cityNumber + 1];
}
}
if(my_rank==0){
MPI_Recv(&ans, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
for(int i=0;i<N;i++){
if(i != N-1){
printf("%d,",*(result+i));
}
else{
printf("%d",*(result+i));
}
}
printf("\n");
printf("Distance: %d\n",ans);
}
if(my_rank!=0){
for (int i = 0 ; i<cityNumber;i++){
current_ans = ans;
while(s.isEmpty(s)){
MPI_Recv(&ans, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
result = tsp(Dis,v,N,1,city_divided[i],0,ans,result,temp,log);
s.Pop(&s,ans);
}
result = tsp(Dis,v,N,1,city_divided[i],0,ans,result,temp,log);
if(current_ans - ans > 0){
s.Push(ans);
MPI_Bcast(&ans,1,MPI_INT,0,MPI_COMM_WORLD);
}
}
}
}
else{
result = tsp(Dis,v,N,1,1,0,sum,result,temp,log);
}
for(int i=0;i<N;i++){
if(i != N-1){
printf("%d,",*(result+i));
}
else{
printf("%d",*(result+i));
}
}
printf("\n");
printf("Distance: %d\n",ans);
return 0;
}
I am trying to solve a special travelling salesman problem with mpi and c. I want make the master processor print the final information including the route and the shorest distance. the other processors with all city group run the tsp function, and when it has the shorter distance, use mpi_send or mpi_Bcast to other processors.Follow this logic, I meet a problem when I run the program.
enter image description here

Dijkstra’s shortest path algorithm in C - changing paramateres problem

this program is made to calculate shortest path between two vertexs. FUnction add_edge is adding edges and also their distance in format add_edge(g, 1, 2, 7);where first parameter is linked list, second and third paramater are edges and last one is distance between them.
Problem is that my program was made to insert it in this format add_edge(g, 'a', 'b', 7);but sending it as integer and in function making it to number with this action int a = a - 'a';.
and after i transformed my code to sending numbers instead of characters it stopped to work. showing no error but returning tash numbers.
Here's old code(working):
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
typedef struct {
int vertex;
int weight;
} edge_t;
typedef struct {
edge_t **edges;
int edges_len;
int edges_size;
int dist;
int prev;
int visited;
} vertex_t;
typedef struct {
vertex_t **vertices;
int vertices_len;
int vertices_size;
} graph_t;
typedef struct {
int *data;
int *prio;
int *index;
int len;
int size;
} heap_t;
void add_vertex (graph_t *g, int i) {
int j;
if (g->vertices_size < i + 1) {
int size = g->vertices_size * 2 > i ? g->vertices_size * 2 : i + 4;
g->vertices = realloc(g->vertices, size * sizeof (vertex_t *));
for (j = g->vertices_size; j < size; j++)
g->vertices[j] = NULL;
g->vertices_size = size;
}
if (!g->vertices[i]) {
g->vertices[i] = calloc(1, sizeof (vertex_t));
g->vertices_len++;
}
}
void add_edge (graph_t *g, int a, int b, int w) {
a = a - 'a';
b = b - 'a';
add_vertex(g, a);
add_vertex(g, b);
vertex_t *v = g->vertices[a];
if (v->edges_len >= v->edges_size) {
v->edges_size = v->edges_size ? v->edges_size * 2 : 4;
v->edges = realloc(v->edges, v->edges_size * sizeof (edge_t *));
}
edge_t *e = calloc(1, sizeof (edge_t));
e->vertex = b;
e->weight = w;
v->edges[v->edges_len++] = e;
}
heap_t *create_heap (int n) {
heap_t *h = calloc(1, sizeof (heap_t));
h->data = calloc(n + 1, sizeof (int));
h->prio = calloc(n + 1, sizeof (int));
h->index = calloc(n, sizeof (int));
return h;
}
void push_heap (heap_t *h, int v, int p) {
int i = h->index[v] == 0 ? ++h->len : h->index[v];
int j = i / 2;
while (i > 1) {
if (h->prio[j] < p)
break;
h->data[i] = h->data[j];
h->prio[i] = h->prio[j];
h->index[h->data[i]] = i;
i = j;
j = j / 2;
}
h->data[i] = v;
h->prio[i] = p;
h->index[v] = i;
}
int min (heap_t *h, int i, int j, int k) {
int m = i;
if (j <= h->len && h->prio[j] < h->prio[m])
m = j;
if (k <= h->len && h->prio[k] < h->prio[m])
m = k;
return m;
}
int pop_heap (heap_t *h) {
int v = h->data[1];
int i = 1;
while (1) {
int j = min(h, h->len, 2 * i, 2 * i + 1);
if (j == h->len)
break;
h->data[i] = h->data[j];
h->prio[i] = h->prio[j];
h->index[h->data[i]] = i;
i = j;
}
h->data[i] = h->data[h->len];
h->prio[i] = h->prio[h->len];
h->index[h->data[i]] = i;
h->len--;
return v;
}
void dijkstra (graph_t *g, int a, int b) {
int i, j;
a = a - 'a';
b = b - 'a';
for (i = 0; i < g->vertices_len; i++) {
vertex_t *v = g->vertices[i];
v->dist = INT_MAX;
v->prev = 0;
v->visited = 0;
}
vertex_t *v = g->vertices[a];
v->dist = 0;
heap_t *h = create_heap(g->vertices_len);
push_heap(h, a, v->dist);
while (h->len) {
i = pop_heap(h);
if (i == b)
break;
v = g->vertices[i];
v->visited = 1;
for (j = 0; j < v->edges_len; j++) {
edge_t *e = v->edges[j];
vertex_t *u = g->vertices[e->vertex];
if (!u->visited && v->dist + e->weight <= u->dist) {
u->prev = i;
u->dist = v->dist + e->weight;
push_heap(h, e->vertex, u->dist);
}
}
}
v = g->vertices[i];
printf("%d\n", v->dist);
}
int main () {
graph_t *g = calloc(1, sizeof (graph_t));
add_edge(g, 'a', 'b', 7);
add_edge(g, 'a', 'c' ,9);
dijkstra(g, 'a', 'c');
return 0;
}
and here is my new code(that i want to make work):
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
typedef struct {
int vertex;
int weight;
} edge_t;
typedef struct {
edge_t **edges;
int edges_len;
int edges_size;
int dist;
int prev;
int visited;
} vertex_t;
typedef struct {
vertex_t **vertices;
int vertices_len;
int vertices_size;
} graph_t;
typedef struct {
int *data;
int *prio;
int *index;
int len;
int size;
} heap_t;
void add_vertex (graph_t *g, int i) {
int j;
if (g->vertices_size < i + 1) {
int size = g->vertices_size * 2 > i ? g->vertices_size * 2 : i + 4;
g->vertices = realloc(g->vertices, size * sizeof (vertex_t *));
for (j = g->vertices_size; j < size; j++)
g->vertices[j] = NULL;
g->vertices_size = size;
}
if (!g->vertices[i]) {
g->vertices[i] = calloc(1, sizeof (vertex_t));
g->vertices_len++;
}
}
void add_edge (graph_t *g, int a, int b, int w) {
add_vertex(g, a);
add_vertex(g, b);
vertex_t *v = g->vertices[a];
if (v->edges_len >= v->edges_size) {
v->edges_size = v->edges_size ? v->edges_size * 2 : 4;
v->edges = realloc(v->edges, v->edges_size * sizeof (edge_t *));
}
edge_t *e = calloc(1, sizeof (edge_t));
e->vertex = b;
e->weight = w;
v->edges[v->edges_len++] = e;
}
heap_t *create_heap (int n) {
heap_t *h = calloc(1, sizeof (heap_t));
h->data = calloc(n + 1, sizeof (int));
h->prio = calloc(n + 1, sizeof (int));
h->index = calloc(n, sizeof (int));
return h;
}
void push_heap (heap_t *h, int v, int p) {
int i = h->index[v] == 0 ? ++h->len : h->index[v];
int j = i / 2;
while (i > 1) {
if (h->prio[j] < p)
break;
h->data[i] = h->data[j];
h->prio[i] = h->prio[j];
h->index[h->data[i]] = i;
i = j;
j = j / 2;
}
h->data[i] = v;
h->prio[i] = p;
h->index[v] = i;
}
int min (heap_t *h, int i, int j, int k) {
int m = i;
if (j <= h->len && h->prio[j] < h->prio[m])
m = j;
if (k <= h->len && h->prio[k] < h->prio[m])
m = k;
return m;
}
int pop_heap (heap_t *h) {
int v = h->data[1];
int i = 1;
while (1) {
int j = min(h, h->len, 2 * i, 2 * i + 1);
if (j == h->len)
break;
h->data[i] = h->data[j];
h->prio[i] = h->prio[j];
h->index[h->data[i]] = i;
i = j;
}
h->data[i] = h->data[h->len];
h->prio[i] = h->prio[h->len];
h->index[h->data[i]] = i;
h->len--;
return v;
}
void dijkstra (graph_t *g, int a, int b) {
int i, j;
for (i = 0; i < g->vertices_len; i++) {
vertex_t *v = g->vertices[i];
v->dist = INT_MAX;
v->prev = 0;
v->visited = 0;
}
vertex_t *v = g->vertices[a];
v->dist = 0;
heap_t *h = create_heap(g->vertices_len);
push_heap(h, a, v->dist);
while (h->len) {
i = pop_heap(h);
if (i == b)
break;
v = g->vertices[i];
v->visited = 1;
for (j = 0; j < v->edges_len; j++) {
edge_t *e = v->edges[j];
vertex_t *u = g->vertices[e->vertex];
if (!u->visited && v->dist + e->weight <= u->dist) {
u->prev = i;
u->dist = v->dist + e->weight;
push_heap(h, e->vertex, u->dist);
}
}
}
v = g->vertices[i];
printf("%d\n", v->dist);
}
int main () {
graph_t *g = calloc(1, sizeof (graph_t));
add_edge(g, 1, 2, 7);
add_edge(g, 1, 3 9);
dijkstra(g, 1, 3);
return 0;
}
I would be happy for any suggestion how to make it work even the old one to transform to accept numbers as paramateres and not 'characters'. thanks guys
John Bollinger's comment is right; you are testing different indices. Inside your dijkstra, you are assuming that the graph is fully populated, when it is really very sparse. If I have it correctly, you can ignore the nil nodes:
## -1,3 +1,4 ##
+#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
## -116,11 +117,17 ##
int i, j;
for (i = 0; i < g->vertices_len; i++) {
vertex_t *v = g->vertices[i];
- v->dist = INT_MAX;
- v->prev = 0;
- v->visited = 0;
+ if (v) {
+ v->dist = INT_MAX;
+ v->prev = 0;
+ v->visited = 0;
+ }
}
vertex_t *v = g->vertices[a];
+ if (v == 0) {
+ printf("-1\n");
+ return;
+ }
v->dist = 0;
heap_t *h = create_heap(g->vertices_len);
push_heap(h, a, v->dist);
## -129,10 +136,12 ##
if (i == b)
break;
v = g->vertices[i];
+ assert(v);
v->visited = 1;
for (j = 0; j < v->edges_len; j++) {
edge_t *e = v->edges[j];
vertex_t *u = g->vertices[e->vertex];
+ assert(u);
if (!u->visited && v->dist + e->weight <= u->dist) {
u->prev = i;
u->dist = v->dist + e->weight;
## -140,8 +149,11 ##
}
}
}
- v = g->vertices[i];
- printf("%d\n", v->dist);
+ if (v = g->vertices[i]) {
+ printf("%d\n", v->dist);
+ } else {
+ printf("-2\n");
+ }
}
At the very least, it doesn't fault with your config.

My BFS code is only showing the direct paths from source to destination, but not all possible paths

I want to print all possible paths from a given source and destination. But in my BFS code, it only shows the two paths, not the multiple path. For a directed graph where n = 4, edge = 6, given,
1-2
1-3
1-5
5-3
5-4
3-4
3-2
It should've printed 3 paths:
1-5-4
1-3-4
1-5-3-4
But it only shows this two paths
1-3-4
1-5-4
This is my sample code for finding the src to destination path
#include <stdio.h>
int queue1[100], state[100], parent[100];
int front = 0, rear = -1, maxSize = 100;
int count = 0;
int initial = 1, waiting = 2, visited = 3;
int n, e;
int adj[100][100];
bool isEmpty()
{
return count == 0;
}
bool isFull()
{
return count == maxSize;
}
void enqueue(int val)
{
if (!isFull())
{
if (rear == maxSize - 1)
{
rear = -1;
}
rear++;
queue1[rear] = val;
count++;
}
}
int dequeue()
{
int val = queue1[front];
front++;
if (front == maxSize)
{
front = 0;
}
count--;
return val;
}
void BFS_Traversal(int src, int des)
{
int done = 0;
enqueue(src);
state[src] = waiting;
parent[src] = -1;
printf("path ");
while (!isEmpty() && done == 0)
{
src = dequeue();
// printf("%d ",src);
state[src] = visited;
for (int i = 1; i <= n; i++)
{
if (adj[src][i] == 1 && state[i] == initial)
{
enqueue(i);
state[i] = waiting;
parent[i] = src;
if (i == des)
{
state[i] = initial;
int k = des;
do
{
printf("%d ", k);
k = parent[k];
} while (k != -1);
printf("\n");
}
}
}
}
}
int main()
{
int src, start, end, des;
scanf("%d%d", &n, &e);
for (int i = 1; i <= e; i++)
{
scanf("%d%d", &start, &end);
adj[start][end] = 1;
}
for (int i = 1; i <= n; i++)
{
state[i] = initial;
}
for (int k = 1; k <= n; k++)
{
parent[k] = -1;
}
scanf("%d%d", &src, &des);
BFS_Traversal(src, des);
}
As, you can see 1-5-3-4 path is not showing because they are already visited. How should I modify this code to print all possible paths?

Runtime Error 402 MASH uva

I just started doing uva problems. However in problem 402 no matter what I do the result of my submission is always runtime error. I cannot understand where is my problem or why is it resulting in errors. Can anyone help, please?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct nodo
{
int info;
struct nodo *sig;
};
struct nodo *p, *q, *nuevo;
void Insertar(int n)
{
int i; //Contador
struct nodo *nuevo;
for(i = 0; i < n; i++)
{
nuevo = (struct nodo*)malloc(sizeof(struct nodo));
nuevo->info = i+1;
if(!p)
p = nuevo;
else
q->sig=nuevo;
q = nuevo;
q->sig = 0;
}
}
void Borrar()
{
struct nodo *aux;
}
int main()
{
int m,s,cont=0,cartas[22],i,j,k;
while(scanf("%i %i",&m,&s)==2)
{
if(cont)
printf("\n");
cont++;
for(i = 0; i < 20; i++)
scanf("%i",&cartas[i]);
printf("Selection #%i\n",cont);
Insertar(m);
for(i = 0, s = m-s; i < 20 && s; i++)
{
j = cartas[i] - 2;
if(j == -1)
{
while(s)
{
nuevo=p->sig;
free(p);
p = nuevo;
s--;
}
}
else
{
nuevo = p;
while(nuevo->sig)
{
for(k = 0; k < j && nuevo->sig; nuevo = nuevo->sig, k++);
if(nuevo->sig)
{
q = nuevo->sig;
nuevo->sig = q->sig;
free(q);
nuevo = nuevo->sig;
s--;
if(!s)
break;
}
}
}
}
printf("%i",p->info);
nuevo = p->sig;
free(p);
while(nuevo)
{
printf(" %i",nuevo->info);
p = nuevo->sig;
free(nuevo);
nuevo = p;
}
printf("\n");
}
}

Resources