-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlsGraph.c
More file actions
187 lines (151 loc) · 3.38 KB
/
lsGraph.c
File metadata and controls
187 lines (151 loc) · 3.38 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/**
* This file implements the functions used for a graph structure
*
* @author Jeffrey Bromen
* @date 4/17/17
* @info Systems and Networks II
* @info Project 3
*/
#include "lsGraph.h"
struct Graph *newGraph(int size, int directed)
{
int i;
struct Graph *graph = (struct Graph *) malloc(sizeof(struct Graph));
if (!graph)
return NULL;
if(!(graph->key = (char *) malloc(size * sizeof(char))))
{
free(graph);
return NULL;
}
for (i = 0; i < size; i++)
graph->key[i] = '\0';
if(!(graph->array = (struct AdjList *) malloc(size * sizeof(struct AdjList))))
{
free(graph->key);
free(graph);
return NULL;
}
for (i = 0; i < size; i++)
graph->array[i].head = NULL;
graph->size = size;
graph->directed = directed;
graph->updated = 0;
return graph;
}
struct AdjListNode *newAdjListNode(int dest, int cost, int seqN)
{
struct AdjListNode *node = (struct AdjListNode *) malloc(sizeof(struct AdjListNode));
if (!node)
return NULL;
node->dest = dest;
node->cost = cost;
node->seqN = seqN;
node->next = NULL;
return node;
}
int updateEdge(struct Graph *graph, int source, int dest, int cost, int seqN)
{
int returnVal = 0;
struct AdjListNode *node = graph->array[source].head;
while (node)
{
if (node->dest == dest)
{
if ((node->seqN + 1) % 256 == seqN)
{
node->cost = cost;
node->seqN = seqN;
graph->updated = 1;
}
returnVal = 1;
break;
}
node = node->next;
}
// If undirected graph, find and update reverse edge as well
if (!graph->directed && returnVal)
{
node = graph->array[dest].head;
while (node)
{
if (node->dest == source)
{
if ((node->seqN + 1) % 256 == seqN)
{
node->cost = cost;
node->seqN = seqN;
}
break;
}
node = node->next;
}
}
return returnVal;
}
int addEdge(struct Graph *graph, char source, char dest, int cost, int seqN)
{
int srcI, destI;
struct AdjListNode *node;
// Get the indices of the source and destination nodes
srcI = getIndex(graph->key, graph->size, source);
destI = getIndex(graph->key, graph->size, dest);
if (srcI < 0 || destI < 0)
return 0;
// Attempt to update existing edge
if (updateEdge(graph, srcI, destI, cost, seqN))
return 1;
// If no existing edge was found, add a new edge
if (!(node = newAdjListNode(destI, cost, seqN)))
return 0;
node->next = graph->array[srcI].head;
graph->array[srcI].head = node;
graph->updated = 1;
// If undirected graph, add reverse edge as well
if (!graph->directed)
{
if (!(node = newAdjListNode(srcI, cost, seqN)))
return 0;
node->next = graph->array[destI].head;
graph->array[destI].head = node;
}
return 1;
}
int getIndex(char *key, int size, char label)
{
int i;
for (i = 0; i < size && key[i]; i++)
{
if (label == key[i])
return i;
}
if (i < size)
{
key[i] = label;
return i;
}
return -1;
}
int addEdgeFromPacket(struct Graph *graph, char *lsPacket)
{
char source = getSourceID(lsPacket);
char dest = getDestinationID(lsPacket);
int cost = getCost(lsPacket);
int seqN = getSequenceNumber(lsPacket);
return addEdge(graph, source, dest, cost, seqN);
}
void printGraph(struct Graph *graph)
{
int i;
struct AdjListNode *node;
for (i = 0; i < graph->size; i++)
{
node = graph->array[i].head;
printf("Vertex '%c' connects to:\n", graph->key[i]);
while (node)
{
printf("\t'%c' at a cost of %d\n", graph->key[node->dest], node->cost);
node = node->next;
}
}
}