forked from doctor-phil/network
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
126 lines (102 loc) · 3.22 KB
/
Copy pathmain.cpp
File metadata and controls
126 lines (102 loc) · 3.22 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
#include <stdio.h>
#include "linked_list.h"
#include "priority_queue.h"
#include "directed_graph.h"
int main(int argc, char** argv)
{
// Instantiating a DirectedGraph struct.
/* DirectedGraph* digraph = initialize_digraph(sizeof(char),"char");
// Creating elements to add to graph
char* a = "a";
char* b = "b";
char* c = "c";
char* d = "d";
char* e = "e";
char* f = "f";
// Adding elements to graph.
add_vertex(digraph, a);
add_vertex(digraph, b);
add_vertex(digraph, c);
add_vertex(digraph, d);
add_vertex(digraph, e);
add_vertex(digraph, f);
// Creating edges between vertices and assigning weights.
add_arc(digraph, a, b, 4);
add_arc(digraph, a, e, 2);
add_arc(digraph, a, f, 1);
add_arc(digraph, b, c, 5);
add_arc(digraph, b, a, 6);
add_arc(digraph, c, e, 4);
add_arc(digraph, d, e, 3);
add_arc(digraph, d, b, 2);
add_arc(digraph, e, d, 3);
add_arc(digraph, e, b, 2);
add_arc(digraph, e, f, 1);
add_arc(digraph, f, d, 3);
*/
/* The code below provides an example of how to:
* Build a DiGraph from a csv file.
* Create, retrieve, and print the adjacency matrix of the DiGraph.
* Print the String representation of the DiGraph to standard output.
* Compute and print the results of the all pairs' shortest paths algorithm (Floyd Warshall).
*/
DirectedGraph* digraph = create_digraph_from_file((char*)"test_adjacency.csv");
// Retrieving a linkedlist of the vertices.
LinkedList* vertexList = get_vertices(digraph);
// Calling the void function to create the adjacency matrix for the digraph.
create_adjacency_matrix(digraph);
// Retrieving the adjacecny matrix as a 2D array.
float** adjMtx = get_adjacency_matrix(digraph);
// Formatting for adjacency matrix printing.
printf(" ");
for(int i = 0; i < linked_list_size(vertexList); i++)
{
Vertex* u = (Vertex*)linked_list_get(vertexList, i);
printf("%d ", *(int*) get_data(u));
}
printf("\n");
// Printing adjacency matrix.
for(int i = 0; i < digraph_size(digraph); i++)
{
Vertex* u = (Vertex*)linked_list_get(vertexList, i);
printf("%d | ", *(int*) get_data(u));
for(int j = 0; j < digraph_size(digraph); j++)
{
Vertex* v = (Vertex*)linked_list_get(vertexList, j);
// If this edge weight is less than my max edge weight, print 0.
if(!(has_arc_to_vertex(u, v)))
{
printf("0 ");
// Otherwise print the edge value.
} else {
printf("%f ", adjMtx[i][j]);
}
}
printf("\n");
}
printf("\n");
// This code prints a String representation of the DiGraph to standard output.
std::cout << *digraph << std::endl;
// Calling all pairs shortest paths algorithm, retrieved as a 2D array.
float** paths = all_pairs_shortest_paths(digraph);
// Formatting for all pairs shortest paths matrix.
printf(" ");
for(int i = 0; i < linked_list_size(vertexList); i++)
{
Vertex* u = (Vertex*)linked_list_get(vertexList, i);
printf("%s ", (char*)get_data(u));
}
printf("\n");
// Printing all pairs shortest paths matrix.
for(int i = 0; i < digraph_size(digraph); i++)
{
Vertex* v = (Vertex*)linked_list_get(vertexList, i);
printf("%s | ",(char*)get_data(v));
for(int j = 0; j < digraph_size(digraph); j++)
{
printf("%.0f ", paths[i][j]);
}
printf("\n");
}
return 0;
}