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
150 lines (112 loc) · 2.77 KB
/
Copy pathexample1.c
File metadata and controls
150 lines (112 loc) · 2.77 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
142
143
144
145
146
147
148
149
150
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
int data;
struct Node *next;
} Node;
typedef struct Graph
{
int vertices;
int *visited;
Node **adjacencyList;
} Graph;
typedef struct Stack
{
int top;
int capacity;
int *array;
} Stack;
/* Creeaza un nod nou */
Node *createNode(int value)
{
Node *newNode = malloc(sizeof(Node));
newNode->data = value;
newNode->next = NULL;
return newNode;
}
/* Creeaza graful */
Graph *createGraph(int vertices)
{
Graph *graph = malloc(sizeof(Graph));
graph->vertices = vertices;
graph->visited = malloc(vertices * sizeof(int));
graph->adjacencyList = malloc(vertices * sizeof(Node *));
for (int i = 0; i < vertices; i++)
{
graph->adjacencyList[i] = NULL;
graph->visited[i] = 0;
}
return graph;
}
/* Adauga muchie */
void addEdge(Graph *graph, int source, int destination)
{
Node *newNode = createNode(destination);
newNode->next = graph->adjacencyList[source];
graph->adjacencyList[source] = newNode;
newNode = createNode(source);
newNode->next = graph->adjacencyList[destination];
graph->adjacencyList[destination] = newNode;
}
/* Creeaza stack */
Stack *createStack(int capacity)
{
Stack *stack = malloc(sizeof(Stack));
stack->capacity = capacity;
stack->top = -1;
stack->array = malloc(capacity * sizeof(int));
return stack;
}
/* Push in stack */
void push(Stack *stack, int value)
{
stack->top++;
stack->array[stack->top] = value;
}
/* DFS */
void DFS(Graph *graph, int vertex)
{
graph->visited[vertex] = 1;
printf("%d ", vertex);
Node *temp = graph->adjacencyList[vertex];
while (temp != NULL)
{
int connectedVertex = temp->data;
if (graph->visited[connectedVertex] == 0)
{
DFS(graph, connectedVertex);
}
temp = temp->next;
}
}
/* Reset visited */
void resetVisited(Graph *graph)
{
for (int i = 0; i < graph->vertices; i++)
{
graph->visited[i] = 0;
}
}
int main()
{
int numberOfVertices;
int numberOfEdges;
printf("Introdu numarul de restaurante: ");
scanf("%d", &numberOfVertices);
printf("Introdu numarul de conexiuni: ");
scanf("%d", &numberOfEdges);
Graph *graph = createGraph(numberOfVertices);
printf("Introdu muchiile:\n");
for (int i = 0; i < numberOfEdges; i++)
{
int source;
int destination;
scanf("%d %d", &source, &destination);
addEdge(graph, source, destination);
}
printf("\nParcurgere DFS:\n");
DFS(graph, 0);
resetVisited(graph);
return 0;
}