Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions graphCountry/graphCountry.sln
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}") = "graphCountry", "graphCountry\graphCountry.vcxproj", "{37B8E2ED-8212-47F8-A168-44A98D5D7568}"
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
{37B8E2ED-8212-47F8-A168-44A98D5D7568}.Debug|x64.ActiveCfg = Debug|x64
{37B8E2ED-8212-47F8-A168-44A98D5D7568}.Debug|x64.Build.0 = Debug|x64
{37B8E2ED-8212-47F8-A168-44A98D5D7568}.Debug|x86.ActiveCfg = Debug|Win32
{37B8E2ED-8212-47F8-A168-44A98D5D7568}.Debug|x86.Build.0 = Debug|Win32
{37B8E2ED-8212-47F8-A168-44A98D5D7568}.Release|x64.ActiveCfg = Release|x64
{37B8E2ED-8212-47F8-A168-44A98D5D7568}.Release|x64.Build.0 = Release|x64
{37B8E2ED-8212-47F8-A168-44A98D5D7568}.Release|x86.ActiveCfg = Release|Win32
{37B8E2ED-8212-47F8-A168-44A98D5D7568}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {7DD259E5-DE48-41D7-844B-628ABC8055CD}
EndGlobalSection
EndGlobal
269 changes: 269 additions & 0 deletions graphCountry/graphCountry/graph.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
#include "graph.h"
#include "list.h"
#include "stack.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>

typedef enum TypeTown {
capital,
notBusy,
busy
}TypeTown;

typedef struct Node {
int vertex;
TypeTown typeTown;
int onWhichControl;
struct Node* next;
int sizeRoadWithMainVertex;
}Node;

typedef struct Graph {
int numNode;
struct Node** nodeList;
int numberOfCapitals;
}Graph;

Graph* createGraph(Error errorCheck, int sizeGraph) {
Graph* graph = calloc(1, sizeof(Graph));
if (graph == NULL) {
errorCheck = memoryError;
return NULL;
}
graph->numNode = sizeGraph;
graph->nodeList = (Node**)malloc(sizeGraph * sizeof(Node));
if (graph->nodeList == NULL) {
errorCheck = memoryError;
free(graph);
return NULL;
}
for (int i = 0; i < sizeGraph; ++i) {
graph->nodeList[i] = calloc(1, sizeof(Node*));
if (graph->nodeList[i] == NULL) {
errorCheck = memoryError;
for (int j = 0; j < i; ++j) {
free(graph->nodeList[j]);
}
free(graph);
return NULL;
}
graph->nodeList[i][0].vertex = i;
graph->nodeList[i][0].next = NULL;
graph->nodeList[i][0].onWhichControl = -1;
graph->nodeList[i][0].typeTown = notBusy;
graph->nodeList[i][0].sizeRoadWithMainVertex = 0;
}

return graph;
}

void helpToAddRoad(Graph* graph, int firstNode, int secondNode, int sizeRoad, Error errorCheck) {
Node* walkerInGraph = graph->nodeList[firstNode];
while (walkerInGraph->next != NULL) {
walkerInGraph = walkerInGraph->next;
}
Node* temp = calloc(1, sizeof(Node));
if (temp == NULL) {
errorCheck = memoryError;
for (int i = 0; i < graph->numNode; ++i) {
free(graph->nodeList[i]);
}
free(graph);
return;
}
temp->sizeRoadWithMainVertex = sizeRoad;
temp->vertex = secondNode;
temp->next = NULL;
temp->onWhichControl = -1;
temp->typeTown = notBusy;
walkerInGraph->next = temp;
}

void addRoad(Graph* graph, Error errorCheck, int firstNode, int secondNode, int sizeRoad) {
if (graph == NULL) {
errorCheck = nullGraph;
return;
}

if (firstNode >= graph->numNode || secondNode >= graph->numNode) {
errorCheck = outOfGraph;
for (int i = 0; i < graph->numNode; ++i) {
free(graph->nodeList[i]);
}
free(graph);
return;
}

if (firstNode == secondNode) {
graph->nodeList[firstNode]->sizeRoadWithMainVertex = sizeRoad;
return;
}

helpToAddRoad(graph, firstNode, secondNode, sizeRoad, errorCheck);
if (errorCheck != ok) {
clearGraph(graph);
return;
}

if (firstNode != secondNode) {
helpToAddRoad(graph, secondNode, firstNode, sizeRoad, errorCheck);
if (errorCheck != ok) {
clearGraph(graph);
return;
}
}
}

void printGraph(Graph* graph) {
if (graph == NULL) {
return;
}
for (int i = 0; i < graph->numNode; ++i) {
Node* walker = graph->nodeList[i];
printf("%d : ", walker->vertex);
if (walker->sizeRoadWithMainVertex != 0) {
printf("-%d- ", walker->sizeRoadWithMainVertex);
}
walker = walker->next;
while (walker != NULL) {
printf("%d ", walker->vertex);
printf("(%d) ", walker->sizeRoadWithMainVertex);
walker = walker->next;
}
printf("\n");
}
}

void addCapital(Graph* graph, int numberCapital, Error checkError) {
if (graph == NULL) {
checkError = anotherError;
return;
}

if (numberCapital >= graph->numNode) {
checkError = anotherError;
clearGraph(graph);
return;
}

graph->nodeList[numberCapital]->typeTown = capital;
Node* walker = graph->nodeList[numberCapital]->next;
while (walker != NULL) {
Node* newWalker = graph->nodeList[walker->vertex];
while (newWalker->vertex != numberCapital) {
newWalker = newWalker->next;
}
newWalker->typeTown = capital;
walker = walker->next;
}
++graph->numberOfCapitals;
}

void recursionAddTown(Graph* graph, int* min, int* numberTown, int index, const int numberCapital, int localMin, Error errorCheck) {
Node* walker = graph->nodeList[index]->next;

while (walker != NULL) {
if (walker->typeTown == busy && walker->onWhichControl == numberCapital) {
localMin += walker->sizeRoadWithMainVertex;
recursionAddTown(graph, min, numberTown, walker->vertex, numberCapital, localMin, errorCheck);
} else if (walker->typeTown == notBusy) {
localMin += walker->sizeRoadWithMainVertex;
if (localMin < *min || *min == -1) {
*min = localMin;
*numberTown = walker->vertex;
}
localMin -= walker->sizeRoadWithMainVertex;
}
walker = walker->next;
}

}

void addTown(Graph* graph, int numberCapital, Error errorCheck, bool* isAdded) {
if (graph == NULL) {
errorCheck = nullGraph;
return;
}
int min = -1;
int numberAddedTown = numberCapital;
int index = numberCapital;
recursionAddTown(graph, &min, &numberAddedTown, index, numberCapital, 0, errorCheck);
if (min != -1) {
graph->nodeList[numberAddedTown]->typeTown = busy;
graph->nodeList[numberAddedTown]->onWhichControl = numberCapital;
Node* walker = graph->nodeList[numberAddedTown]->next;
while (walker != NULL) {
Node* anotherWalker = graph->nodeList[walker->vertex];
while (anotherWalker->vertex != numberAddedTown) {
anotherWalker = anotherWalker->next;
}
anotherWalker->typeTown = busy;
anotherWalker->onWhichControl = numberCapital;
walker = walker->next;
}
*isAdded = true;
}
}

void toControllTowns(Graph* graph, Error errorCheck) {
if (graph == NULL) {
errorCheck = nullGraph;
return;
}
int i = 0;
int indexCapital = 0;
bool isAdded = false;
while (i < graph->numNode - graph->numberOfCapitals) {
while (graph->nodeList[indexCapital]->typeTown != capital) {
++indexCapital;
if (indexCapital == graph->numNode) {
indexCapital = 0;
}
}
isAdded = false;
addTown(graph, indexCapital, errorCheck, &isAdded);
++indexCapital;
if (indexCapital == graph->numNode) {
indexCapital = 0;
}
if (isAdded) {
++i;
}
}
}

void returnArrayWithControlledTowns(Graph* graph, Error errorCheck, bool** controlledTownsArray) {
int indexSizeTowns = 0;
Node* walker = graph->nodeList[0];
while (indexSizeTowns < graph->numNode) {
while (walker != NULL) {
if (walker->typeTown == capital) {
controlledTownsArray[walker->vertex][0] = true;
}
else {
controlledTownsArray[walker->onWhichControl][walker->vertex + 1] = true;
}
walker = walker->next;
}
walker = graph->nodeList[indexSizeTowns];
++indexSizeTowns;
}
return controlledTownsArray;
}

void clearGraph(Graph* graph) {
int i = 0;
while (i < graph->numNode) {
Node* walker = graph->nodeList[i]->next;
while (walker != NULL) {
Node* temp = walker->next;
free(walker);
walker = temp;
}
// free(graph->nodeList[i]);
++i;
}
free(graph->nodeList);
free(graph);
}
26 changes: 26 additions & 0 deletions graphCountry/graphCountry/graph.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once
#include <stdbool.h>

typedef struct Graph Graph;
typedef enum Error {
memoryError,
anotherError,
nullGraph,
outOfGraph,
fileError,
ok,
}Error;

Graph* createGraph(Error errorCheck, int sizeGraph);

void addRoad(Graph* graph, Error errorCheck, int firstNode, int secondNode, int sizeRoad);

void printGraph(Graph* graph);

void toControllTowns(Graph* graph, Error errorCheck);

void addCapital(Graph* graph, int numberCapital, Error checkError);

void returnArrayWithControlledTowns(Graph* graph, Error errorCheck, bool** controlledTownsArray);

void clearGraph(Graph* graph);
Loading