-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from artemiipatov/graph
Graph
- Loading branch information
Showing
14 changed files
with
904 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 17 | ||
VisualStudioVersion = 17.0.31903.59 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "states", "states\states.vcxproj", "{91B174A2-4331-45C8-B49E-E9ABF1F483EB}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|x64 = Debug|x64 | ||
Debug|x86 = Debug|x86 | ||
Release|x64 = Release|x64 | ||
Release|x86 = Release|x86 | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{91B174A2-4331-45C8-B49E-E9ABF1F483EB}.Debug|x64.ActiveCfg = Debug|x64 | ||
{91B174A2-4331-45C8-B49E-E9ABF1F483EB}.Debug|x64.Build.0 = Debug|x64 | ||
{91B174A2-4331-45C8-B49E-E9ABF1F483EB}.Debug|x86.ActiveCfg = Debug|Win32 | ||
{91B174A2-4331-45C8-B49E-E9ABF1F483EB}.Debug|x86.Build.0 = Debug|Win32 | ||
{91B174A2-4331-45C8-B49E-E9ABF1F483EB}.Release|x64.ActiveCfg = Release|x64 | ||
{91B174A2-4331-45C8-B49E-E9ABF1F483EB}.Release|x64.Build.0 = Release|x64 | ||
{91B174A2-4331-45C8-B49E-E9ABF1F483EB}.Release|x86.ActiveCfg = Release|Win32 | ||
{91B174A2-4331-45C8-B49E-E9ABF1F483EB}.Release|x86.Build.0 = Release|Win32 | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {F8095D37-B50D-411B-A4BA-BADD003114F2} | ||
EndGlobalSection | ||
EndGlobal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
#define _CRT_SECURE_NO_WARNINGS | ||
#include <stdbool.h> | ||
#include <stdlib.h> | ||
#include "graph.h" | ||
|
||
typedef struct Graph | ||
{ | ||
int numberOfNodes; | ||
int matrix[NUMBER_OF_NODES][NUMBER_OF_NODES]; | ||
} Graph; | ||
|
||
void deleteGraph(Graph** graph) | ||
{ | ||
free(*graph); | ||
*graph = NULL; | ||
} | ||
|
||
int getEdge(Graph* graph, const int start, const int end) | ||
{ | ||
return graph->matrix[start][end]; | ||
} | ||
|
||
void setEdge(Graph* graph, const int firstNode, const int secondNode, const int weight) | ||
{ | ||
graph->matrix[firstNode][secondNode] = weight; | ||
graph->matrix[secondNode][firstNode] = weight; | ||
} | ||
|
||
Graph* buildGraph(FILE* input) | ||
{ | ||
Graph* graph = calloc(1, sizeof(Graph)); | ||
if (graph == NULL) | ||
{ | ||
return NULL; | ||
} | ||
graph->numberOfNodes = 0; | ||
int numberOfNodes = 0; | ||
int numberOfRoads = 0; | ||
fscanf_s(input, "%d", &numberOfNodes); | ||
graph->numberOfNodes = numberOfNodes; | ||
fscanf_s(input, "%d", &numberOfRoads); | ||
for (int index = 0; index < numberOfRoads; index++) | ||
{ | ||
int nodeIndex1 = 0; | ||
int nodeIndex2 = 0; | ||
int roadLength = 0; | ||
fscanf_s(input, "%d", &nodeIndex1); | ||
fscanf_s(input, "%d", &nodeIndex2); | ||
fscanf_s(input, "%d", &roadLength); | ||
if (nodeIndex1 >= NUMBER_OF_NODES || nodeIndex2 >= NUMBER_OF_NODES) | ||
{ | ||
printf("City index should be less than %d", NUMBER_OF_NODES); | ||
deleteGraph(&graph); | ||
return NULL; | ||
} | ||
setEdge(graph, nodeIndex1, nodeIndex2, roadLength); | ||
} | ||
return graph; | ||
} | ||
|
||
void deleteColumn(Graph* graph, const int column) | ||
{ | ||
for (int index = 0; index < NUMBER_OF_NODES; index++) | ||
{ | ||
graph->matrix[index][column] = 0; | ||
} | ||
} | ||
|
||
int getNumberOfNodes(Graph* graph) | ||
{ | ||
return graph->numberOfNodes; | ||
} | ||
|
||
void getMatrix(Graph* graph, int matrix[NUMBER_OF_NODES][NUMBER_OF_NODES]) | ||
{ | ||
for (int row = 0; row < NUMBER_OF_NODES; row++) | ||
{ | ||
for (int column = 0; column < NUMBER_OF_NODES; column++) | ||
{ | ||
matrix[row][column] = graph->matrix[row][column]; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#pragma once | ||
#include <stdio.h> | ||
#include "list.h" | ||
#define NUMBER_OF_NODES 20 | ||
|
||
// graph structure | ||
typedef struct Graph Graph; | ||
|
||
// deletes graph | ||
void deleteGraph(Graph** graph); | ||
|
||
// returns length of edge between two cities | ||
int getEdge(Graph* graph, const int start, const int end); | ||
|
||
// parses file | ||
Graph* buildGraph(FILE* input); | ||
|
||
// deletes whole column | ||
void deleteColumn(Graph* graph, const int column); | ||
|
||
// returns number of graph nodes | ||
int getNumberOfNodes(Graph* graph); | ||
|
||
// copies adjacency matrix to 2d array | ||
void getMatrix(Graph* graph, int matrix[NUMBER_OF_NODES][NUMBER_OF_NODES]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#include <stdlib.h> | ||
#include "graphTests.h" | ||
#include "states.h" | ||
|
||
bool testCapturing(void) | ||
{ | ||
States* states = readFile("inputTest.txt"); | ||
if (states == NULL) | ||
{ | ||
return false; | ||
} | ||
if (!capture(states)) | ||
{ | ||
deleteStates(&states); | ||
return false; | ||
} | ||
List** arrayOfStates = getCapitals(states); | ||
int numberOfCapitals = getNumberOfCapitals(states); | ||
int correctStates[3][4] = { { 6, 3, 0, 7 }, { 4, 5 }, { 2, 1 } }; | ||
for (int i = 0; i < numberOfCapitals; i++) | ||
{ | ||
Position* position = createPosition(); | ||
if (position == NULL) | ||
{ | ||
deleteStates(&states); | ||
return false; | ||
} | ||
first(arrayOfStates[i], &position); | ||
int column = 0; | ||
while (!isNull(position)) | ||
{ | ||
if (getPositionValue(position) != correctStates[i][column]) | ||
{ | ||
deletePosition(&position); | ||
deleteStates(&states); | ||
return false; | ||
} | ||
next(&position); | ||
++column; | ||
} | ||
deletePosition(&position); | ||
} | ||
deleteStates(&states); | ||
return states == NULL; | ||
} | ||
|
||
bool testParsing(void) | ||
{ | ||
States* states = readFile("inputTest.txt"); | ||
if (states == NULL) | ||
{ | ||
return false; | ||
} | ||
int matrix[NUMBER_OF_NODES][NUMBER_OF_NODES] = { 0 }; | ||
getMatrix(getGraph(states), matrix); | ||
if (!(getNumberOfCities(states) == 8 && getNumberOfCapitals(states) == 3)) | ||
{ | ||
deleteStates(&states); | ||
return false; | ||
} | ||
if (!(matrix[0][1] == matrix[1][0] && matrix[0][1] == 7 | ||
&& matrix[1][2] == matrix[2][1] && matrix[1][2] == 5 | ||
&& matrix[2][3] == matrix[3][2] && matrix[2][3] == 8 | ||
&& matrix[3][4] == matrix[4][3] && matrix[3][4] == 21 | ||
&& matrix[4][5] == matrix[5][4] && matrix[4][5] == 1 | ||
&& matrix[4][0] == matrix[0][4] && matrix[4][0] == 2 | ||
&& matrix[5][6] == matrix[6][5] && matrix[5][6] == 9 | ||
&& matrix[6][0] == matrix[0][6] && matrix[6][0] == 3 | ||
&& matrix[6][1] == matrix[1][6] && matrix[6][1] == 10 | ||
&& matrix[6][2] == matrix[2][6] && matrix[6][2] == 13 | ||
&& matrix[6][3] == matrix[3][6] && matrix[6][3] == 1 | ||
&& matrix[6][4] == matrix[4][6] && matrix[6][4] == 6 | ||
&& matrix[6][7] == matrix[7][6] && matrix[6][7] == 34)) | ||
{ | ||
deleteStates(&states); | ||
return false; | ||
} | ||
deleteStates(&states); | ||
return states == NULL; | ||
} | ||
|
||
bool graphPassedTests(void) | ||
{ | ||
return testParsing() && testCapturing(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#pragma once | ||
#include <stdbool.h> | ||
|
||
// graph tests master, returns true if graph passed all tests, false if it did not pass at least one test | ||
bool graphPassedTests(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
8 20 | ||
0 1 6 | ||
1 2 7 | ||
2 3 3 | ||
3 4 1 | ||
4 5 9 | ||
5 6 2 | ||
6 7 9 | ||
7 0 10 | ||
5 0 2 | ||
3 6 1 | ||
5 7 3 | ||
5 2 8 | ||
5 1 9 | ||
2 4 10 | ||
1 3 2 | ||
1 4 1 | ||
7 4 5 | ||
3 7 4 | ||
3 0 19 | ||
0 4 10 | ||
4 | ||
2 4 5 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
8 13 | ||
0 1 7 | ||
1 2 5 | ||
2 3 8 | ||
3 4 21 | ||
4 5 1 | ||
4 0 2 | ||
5 6 9 | ||
6 0 3 | ||
6 1 10 | ||
6 2 13 | ||
6 3 1 | ||
6 4 6 | ||
6 7 34 | ||
3 | ||
6 4 2 |
Oops, something went wrong.