Facebook
From group 144, 3 Years ago, written in C.
Embed
Download Paste or View Raw
Hits: 143
  1. #include "graph.h"
  2. #include <stdbool.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. struct Edge {
  7.     int start;
  8.     int end;
  9.     int weight;
  10.     bool oriented;
  11. };
  12.  
  13. struct Graph {
  14.     int** matrix;
  15.     int countVertex;
  16.     int countEdges;
  17. };
  18.  
  19. Edge* createEdge(int start, int end, int weight, bool oriented) {
  20.     Edge* edge = malloc(sizeof(Edge));
  21.     edge->start = start;
  22.     edge->end = end;
  23.     edge->weight = weight;
  24.     edge->oriented = oriented;
  25.     return edge;
  26. }
  27.  
  28. Graph* createGraph(int countEdges, int countVertex, Edge** edges) {
  29.     Graph* graph = (Graph*)malloc(sizeof(Graph));
  30.     graph->countVertex = countVertex;
  31.     graph->countEdges = countEdges;
  32.     graph->matrix = (int**)malloc(countVertex * sizeof(int*));
  33.     for (int i = 0; i < countVertex; ++i) {
  34.         graph->matrix[i] = (int*)malloc(countVertex * sizeof(int));
  35.         memset(graph->matrix[i], 0, countVertex * sizeof(int));
  36.     }
  37.     for (int i = 0; i < countEdges; ++i) {
  38.         graph->matrix[edges[i]->start][edges[i]->end] = edges[i]->weight;
  39.         if (!edges[i]->oriented) {
  40.             graph->matrix[edges[i]->end][edges[i]->start] = edges[i]->weight;
  41.         }
  42.     }
  43.     return graph;
  44. }
  45.  
  46. bool isConnected(int fromVertex, int toVertex, Graph* graph)
  47. {
  48.     int* vertexState = (int*)malloc(graph->countVertex*sizeof(int));
  49.     memset(vertexState,0,graph->countVertex*sizeof(int));
  50.     depthFirstSearch(graph, fromVertex, vertexState);
  51.     return vertexState[toVertex] > 0;
  52. }
  53.  
  54. bool depthFirstSearch(Graph* graph, int currentVertex, int* vertexState)
  55. {
  56.     vertexState[currentVertex] = 1;
  57.     for (int i = 0; i < graph->countVertex; i++) {
  58.         if (graph->matrix[currentVertex][i] != 0)
  59.         {
  60.             if(vertexState[i] == 1) {
  61.                 return true;
  62.             }
  63.             if (vertexState[i] == 0){
  64.                 if(depthFirstSearch(graph, i, vertexState))
  65.                 {
  66.                     return true;
  67.                 }
  68.             }
  69.         }
  70.     }
  71.     vertexState[currentVertex] = 2;
  72.     return false;
  73. }
  74.  
  75. bool isCycled(Graph* graph)
  76. {
  77.     int* vertexState = (int*)malloc(graph->countVertex*sizeof(int));
  78.     memset(vertexState,0,graph->countVertex*sizeof(int));
  79.     for(int i = 0; i < graph->countVertex; ++i)
  80.     {
  81.         if(vertexState[i] == 0)
  82.         {
  83.             if(depthFirstSearch(graph, i, vertexState))
  84.             {
  85.                 return
  86.             };
  87.         }
  88.         if(vertexState[i])
  89.     }
  90. }
  91.  

Replies to graph.c rss

Title Name Language When
Re: graph.c group 144 c 3 Years ago.