Пример #1
0
//Made by Bruno Emori (RA 88736) & Christian Nakata (RA90558)
//Algoritmos em Grafos - Prof Rodrigo Calvo
//Universidade Estadual de Maringá - 2016
int main() {
  int nVertex, nEdges, graphType, v1, v2, edgeWeight, count;
  fscanf(stdin, "%i", &nVertex);
  fscanf(stdin, "%i", &nEdges);
  fscanf(stdin, "%i", &graphType);

  if ((graphType != 0) && (graphType != 1)) {
    printf("Graph type must be (0) - Undirected graph, or (1) - Directed graph.\n");
    return 0;
  }
 
  adjListBlock *adjList[nVertex];
  createAdjList(adjList, nVertex);

  while (fscanf(stdin, "%i", &v1) != EOF) {
    fscanf(stdin, "%i", &v2);
    fscanf(stdin, "%i", &edgeWeight);
    insertAdjList(adjList, v1, v2, edgeWeight);
    if (!graphType)
     insertAdjList(adjList, v2, v1, edgeWeight);
  }

  printf("Number of vertices: %i.\n", nVertex);
  printf("Number of edges: %i.\n", nEdges);
  if (!graphType)
    printf("Graph Type: Undirected Graph\n");
  else
    printf("Graph Type: Directed Graph\n");
  
  printf("\nEdges:\n");
  for (count = 0; count < nVertex; count++) {
    printf("Vertex %i: ", count);
    printAdjList(adjList, count);
    printf("\n");
  }

  printf("\n\nDeep First Search:\n");
  deepFirstSearch(adjList, nVertex);

  printf("\n\nBreadth First Search:\n");
  breadthFirstSearch(adjList, nVertex, 0);

  printf("\n\nComponents:\n");
  connectedComponent(adjList, nVertex);
  
  if (graphType) {
    printf("\n\nShortest path: (Using Dijkstra's Algorithm)\n");
    dijkstra(adjList, nVertex, 0);
  }
  
  printf("End Program.\n");
  return 0;
}
Пример #2
0
int main()
{
    FILE * f = fopen("matrix.txt","r");
    readFromAdjMatrix(f);
    int V;
    printf("give the number of vertices of the graph:\n");
    scanf("%d",&V);
    struct Graph* graph = createGraph(V);
    FromMatrixToList(graph);
    printGraph(graph);
    createAdjList(adjMatrix);
    int** newAdjMatr = ListToMatrix(adjList, adjListlength);
   printAdjMatrix(newAdjMatr);

    return 0;
}
Пример #3
0
/***
Adds connectionNode, time and line to Nodes adjList. If connectionNode already exist, line is added to the lineList of the existing entry in adjList
\param Node The node that has an adjList that will be extended with the a new element.
\param connectionNode The node that will be part of the new element in adjList.
\param time The time that will be part of the new element in adjList.
\param line The line that will be part of the new element in adjList.
\return Nothing

*/
void addToAdjList (struct node *Node, struct node *connectionNode, unsigned short time, unsigned short line){
  
  int write = 1;
  struct adjList **dp = &(Node->connections);
  while (*dp != NULL) { 
    if ((strncmp ((*dp)->node->name, connectionNode->name, 100) == 0)) {
      addLine ((*dp)->lines, line);
      write = 0;
      break;
    }
    dp = &((*dp)->next);
  }
  if (write == 1) {
    *dp = (createAdjList(connectionNode, time, line));
  }
}
Пример #4
0
int main()
{
    FILE * f = fopen("matrix.txt","r");
    readFromAdjMatrix(f);
    printAdjMatrix();
    adjList=createAdjList();
    printAdjList(adjList);
    bfs(0);
    dfs(0);
    printf("\n");
    printf("DFS Recursive\n");
    dfsRecurs(0);
    printf("\n");
    recreateAdjMatrix();
    printAdjMatrixAfter();
    return 0;
}
Пример #5
0
void  findCycles()
//
//  Input:   none
//  Output:  none
//  Purpose: finds all cycles in the drainage network (i.e., closed loops that
//           start and end at the same node).
//
{
    int i;

    // --- allocate arrays
    AdjList  = (int *) calloc(2*Nobjects[LINK], sizeof(int));
    StartPos = (int *) calloc(Nobjects[NODE], sizeof(int));
    Stack    = (int *) calloc(Nobjects[NODE], sizeof(int));
    Examined = (char *) calloc(Nobjects[NODE], sizeof(char));
    InTree   = (char *) calloc(Nobjects[LINK], sizeof(char));
    LoopLinks = (int *) calloc(Nobjects[LINK], sizeof(int));
    if ( StartPos && AdjList && Stack && Examined && InTree && LoopLinks )
    {
        // --- create an undirected adjacency list for the nodes
        createAdjList(UNDIRECTED);

        // --- set to empty the list of nodes examined and the list
        //     of links in the spanning tree
        for ( i=0; i<Nobjects[NODE]; i++) Examined[i] = 0;
        for ( i=0; i<Nobjects[LINK]; i++) InTree[i] = 0;

        // --- find a spanning tree for each unexamined node
        //     (cycles are identified as tree is constructed)
        for ( i=0; i<Nobjects[NODE]; i++)
        {
            if ( Examined[i] ) continue;
            Last = -1;
            findSpanningTree(i);
        }
    }
    FREE(StartPos);
    FREE(AdjList);
    FREE(Stack);
    FREE(Examined);
    FREE(InTree);
    FREE(LoopLinks);
}
Пример #6
0
void toposort_sortLinks(int sortedLinks[])
//
//  Input:   none
//  Output:  sortedLinks = array of link indexes in sorted order
//  Purpose: sorts links from upstream to downstream.
//
{
    int i, n = 0;

    // --- no need to sort links for Dyn. Wave routing
    for ( i=0; i<Nobjects[LINK]; i++) sortedLinks[i] = i;
    if ( RouteModel == DW )
    {
        // --- find number of outflow links for each node
        for ( i=0; i<Nobjects[NODE]; i++ ) Node[i].degree = 0;
        for ( i=0; i<Nobjects[LINK]; i++ )
        {
            // --- if upstream node is an outfall, then increment outflow
            //     count for downstream node, otherwise increment count
            //     for upstream node
            n = Link[i].node1;
            if ( Node[n].type == OUTFALL ) Node[ Link[i].node2 ].degree++;
            else Node[n].degree++;
        }
        return;
    }

    // --- allocate arrays used for topo sorting
    if ( ErrorCode ) return;
    InDegree = (int *) calloc(Nobjects[NODE], sizeof(int));
    StartPos = (int *) calloc(Nobjects[NODE], sizeof(int));
    AdjList  = (int *) calloc(Nobjects[LINK], sizeof(int));
    Stack    = (int *) calloc(Nobjects[NODE], sizeof(int));
    if ( InDegree == NULL || StartPos == NULL ||
         AdjList == NULL || Stack == NULL )
    {
        report_writeErrorMsg(ERR_MEMORY, "");
    }
    else
    {
        // --- create a directed adjacency list of links leaving each node
        createAdjList(DIRECTED);

        // --- adjust adjacency list for DIVIDER nodes
        adjustAdjList();

        // --- find number of links entering each node
        for (i = 0; i < Nobjects[NODE]; i++) InDegree[i] = 0;
        for (i = 0; i < Nobjects[LINK]; i++) InDegree[ Link[i].node2 ]++;

        // --- topo sort the links
        n = topoSort(sortedLinks);
    }   

    // --- free allocated memory
    FREE(InDegree);
    FREE(StartPos);
    FREE(AdjList);
    FREE(Stack);

    // --- check that all links are included in SortedLinks
    if ( !ErrorCode &&  n != Nobjects[LINK] )
    {
        report_writeErrorMsg(ERR_LOOP, "");
        findCycles();
    }
}