Академический Документы
Профессиональный Документы
Культура Документы
Отчёт
О выполнении лабораторной работы №4
По Дискретной Математике.
Тема: Использование структур данных для поиска на
деревьях и графах. Алгоритм поиска Форда.
Кишинёв, 2024.
Цель работы: Изучение алгоритмов поиска минимального пути в
графе. Разработка программы, реализующей данные алгоритмы
поиска минимального пути в взвешенном графе.
return graph;
}
if (graph->adjacencyList[start] == NULL)
{
graph->adjacencyList[start] = newNode;
}
else
{
Node *current = graph->adjacencyList[start];
while (current->next != NULL)
{
current = current->next;
}
current->next = newNode;
}
}
void displayGraph(Graph *graph)
{
printf("\n");
for (int i = 0; i < graph->vertices; i++)
{
Node *current = graph->adjacencyList[i];
printf("%d:", i + 1);
while (current != NULL)
{
printf(" %d (%d) [%d]", current->vertex + 1, current->link + 1,
current->weight);
if (current->next != NULL)
{
printf(", ");
}
current = current->next;
}
printf("\n");
}
}
void findMinPaths(Graph *graph, int start, int dest, int *visited, int *nodes,
int pathLength, int pathDistance, int *minDistance, Path **paths)
{
visited[start] = true;
nodes[pathLength] = start;
pathLength++;
if (start == dest)
{
if (pathDistance < *minDistance)
{
*minDistance = pathDistance;
freePaths(*paths);
*paths = NULL;
}
if (pathDistance == *minDistance)
{
addPath(paths, nodes, pathLength, pathDistance);
}
}
else
{
Node *current = graph->adjacencyList[start];
while (current != NULL)
{
if (!visited[current->vertex])
{
findMinPaths(graph, current->vertex, dest, visited, nodes,
pathLength, pathDistance + current->weight, minDistance, paths);
}
current = current->next;
}
}
visited[start] = false;
}
void findMaxPaths(Graph *graph, int start, int dest, int *visited, int *nodes,
int pathLength, int pathDistance, int *maxDistance, Path **paths)
{
visited[start] = true;
nodes[pathLength] = start;
pathLength++;
if (start == dest)
{
if (pathDistance > *maxDistance)
{
freePaths(*paths);
*paths = NULL;
*maxDistance = pathDistance;
}
if (pathDistance == *maxDistance)
{
addPath(paths, nodes, pathLength, pathDistance);
}
}
else
{
Node *current = graph->adjacencyList[start];
while (current != NULL)
{
if (!visited[current->vertex])
{
findMaxPaths(graph, current->vertex, dest, visited, nodes,
pathLength, pathDistance + current->weight, maxDistance, paths);
}
current = current->next;
}
}
visited[start] = false;
}
free(visited);
free(nodes);
freePaths(paths);
}
free(visited);
free(nodes);
freePaths(paths);
}
int main()
{
printf("Enter number of vertices: ");
int vertices;
scanf("%d", &vertices);
printf("Enter edges in the format from start to end point with the weight of
the link between them:\n");
for (int i = 0; i < edges; i++)
{
int start, end, weight;
scanf("%d %d %d", &start, &end, &weight);
addEdge(graph, start - 1, end - 1, weight);
}
int option;
do
{
printf("\n");
printf("Choose an option:\n");
printf("1. Show min path.\n");
printf("2. Show max path.\n");
printf("3. Exit.\n");
scanf("%d", &option);
switch (option)
{
case 1:
findMinPathsWithFord(graph, startVertex, destVertex);
break;
case 2:
findMaxPathsWithFord(graph, startVertex, destVertex);
break;
case 3:
return 0;
default:
printf("Wrong option\n");
break;
}
return 0;
}