forked from MirceaOvidiu/CleanCoding-GIT-PA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample1.c
More file actions
141 lines (109 loc) · 2.78 KB
/
Copy pathexample1.c
File metadata and controls
141 lines (109 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* Determinati daca exista sau nu drum direct intre doua restaurante dintr-o retea de tip graf */
#include <stdlib.h>
#include <stdio.h>
typedef struct Node {
int data;
struct Node* next;
} NODE;
/// pentru simplitate, folosim int-uri pt a numi restaurantele/locatiile
/// ex: 1 - restaurantul 1 si tot asa
typedef struct g {
int v;
int* vis;
struct Node** alst;
} GPH;
typedef struct s {
int t;
int scap;
int* arr;
} STK;
NODE* create_node(int v) {
NODE* nn = malloc(sizeof(NODE));
nn->data = v;
nn->next = NULL;
return nn;
}
void add_edge(GPH* g, int src, int dest) {
NODE* nn = create_node(dest);
nn->next = g->alst[src];
g->alst[src] = nn;
nn = create_node(src);
nn->next = g->alst[dest];
g->alst[dest] = nn;
}
GPH* create_g(int v) {
GPH* g = malloc(sizeof(GPH));
g->v = v;
g->alst = malloc(sizeof(NODE*) * v);
g->vis = malloc(sizeof(int) * v);
for (int i = 0; i < v; i++) {
g->alst[i] = NULL;
g->vis[i] = 0;
}
return g;
}
STK* create_s(int scap) {
STK* s = malloc(sizeof(STK));
s->arr = malloc(scap * sizeof(int));
s->t = -1;
s->scap = scap;
return s;
}
void push(int pshd, STK* s) {
s->t = s->t + 1;
s->arr[s->t] = pshd;
}
void DFS(GPH* g, STK* s, int v_nr) {
NODE* aux = g->alst[v_nr];
g->vis[v_nr] = 1;
push(v_nr, s);
while (aux != NULL) {
int con_ver = aux->data;
if (g->vis[con_ver] == 0) {
DFS(g, s, con_ver);
}
aux = aux->next;
}
}
void insert_edges(GPH* g, int edg_nr, int nrv) {
int src, dest;
printf("Adauga %d muchii (de la 0 la %d):\n", edg_nr, nrv - 1);
for (int i = 0; i < edg_nr; i++) {
scanf("%d%d", &src, &dest);
add_edge(g, src, dest);
}
}
void wipe(GPH* g, int nrv) {
for (int i = 0; i < nrv; i++) {
g->vis[i] = 0;
}
}
int canbe(GPH* g, int src, int dest, int nrv) {
STK* s = create_s(nrv);
DFS(g, s, src);
for (int i = 0; i <= s->t; i++) {
if (s->arr[i] == dest) {
return 1;
}
}
return 0;
}
int main() {
int nrv;
int edg_nr;
int src, dest;
printf("Cate noduri are graful? ");
scanf("%d", &nrv);
printf("Cate muchii are graful? ");
scanf("%d", &edg_nr);
GPH* g = create_g(nrv);
insert_edges(g, edg_nr, nrv);
printf("Introdu perechea de noduri pentru verificare (src dest): ");
scanf("%d%d", &src, &dest);
if (canbe(g, src, dest, nrv)) {
printf("Drumul direct exista intre restaurante.\n");
} else {
printf("Nu exista drum direct intre restaurante.\n");
}
return 0;
}