Пример #1
0
/********************************************************************
  Lettura del file di input ed allocazione del grafo
********************************************************************/
Graph graph_read(FILE *fin, TS computers, int nC)
{
    char input_line[MAX_LEN];
    char src_name[MAX_NAME], dst_name[MAX_NAME];
    int i, speed, src, dst;
    EdgeWeight ew;
    Graph graph;

    graph = GRAPHinit(nC);

    while ( fgets(input_line, MAX_LEN, fin) != NULL)
    {
        for (i=0; i<nC; i++)
        {
            sscanf(input_line, "%s %s %d", src_name, dst_name, &speed);
            ew.speed = speed;
            src = TScercaOAggiungi(computers, src_name);
            dst = TScercaOAggiungi(computers, dst_name);
            if (src == -1 || dst == -1)
            {
                printf("Errore di inserzione nel grafo\n");
                fclose(fin);
                exit(FAILURE);
            }
            GRAPHinsertE(graph, src, dst, ew);
        }
    }
    return graph;
}
Пример #2
0
int bfs(void)
{
  Graph gr;
  Slist network;
  struct BfsVertexdata_ bfstmp;
  BfsVertexdata netdata;
  int tmpid, retval;
  SlistNode listnode;

  my_clearscrn();
  printf("--- NETWORK HOPS/BFS DEMO ---");

  gr = GRAPHinit(bfs_match, bfs_destroy);

  /* Read net node(=vertex) data into graph.. */
  if ((read_netnodes(gr, net_nodes, NR_OF_NETNODES)) != OK)
    {
      fprintf(stderr, "Fatal error when reading netnode data - bailing out..");
      GRAPHdestroy(gr);
      exit(-1);
    }
  /* Read net connection(=edge) data into graph.. */
  if ((read_netconnections(gr, net_connections, NR_OF_NETNODES)) != OK)
    {
      fprintf(stderr, "Fatal error when reading netconnections data - bailing out..");
      GRAPHdestroy(gr);
      exit(-1);
    }

  prompt_and_pause("\n\nGraph initialized and graph data read..");
  printf("\nGRAPH:");

  /* Display graph.. */
  GRAPHprint(gr, prt_bfs_vtx, prt_bfs_edge);
  printf("\nNr of vertices/edges: %d/%d", GRAPHvcount(gr), GRAPHecount(gr));

  tmpid = read_int("\nEnter startnode id ", 1, 6);
  bfstmp.data = &tmpid;

  if ((retval = ALGObfs(gr, &bfstmp, &network, bfs_match)) != OK)
    {
      fprintf(stderr, "\nFatal error when calling 'ALGObfs()'(Return value: %d) - Bailing out..", retval);
      GRAPHdestroy(gr);
      exit(-1);
    }

  printf("\nNetwork Hops(BFS Analysis)\n--------------------------");
  for (listnode = SLISThead(network); listnode != NULL; listnode = SLISTnext(listnode))
    {
      netdata = (BfsVertexdata)SLISTdata(listnode);
      printf("\nNode%02d, color=%d, hops=%d", *(int *)netdata->data, netdata->color, netdata->hops);
    }
    
  prompt_and_pause("\n\nTime to tidy up..");

  SLISTdestroy(network);
  GRAPHdestroy(gr);
 
  return OK;
}
Пример #3
0
Graph generate_network (char *filename)
{
    FILE *finn;
    int n;
    int v, w;
    double weight;
    Edge e;
    Graph G = malloc(sizeof(Graph));

    finn = fopen(filename, "r");
    if (finn == NULL)
    {
        fprintf(stderr, "Error in opening file %s.\n", filename);
        exit(EXIT_FAILURE);
    }

    fscanf (finn, "%d", &n);

    G = GRAPHinit(n);

    while ( (fscanf(finn, "%d-%d %lf", &v, &w, &weight)) > 0 )
    {
        e = EDGE(v, w, weight);
        GRAPHinsertE(G, e);
    }

    fclose(finn);
    return G;

}
Пример #4
0
int main(int argc, char *argv[]) { 
    Edge e, *edges;
    Graph g;
    int graphSize, i, noOfEdges;

    if (argc < 2) {
        printf("Setting max. no of vertices to %d\n", MAX_VERT);
        graphSize = MAX_VERT;
    } else  { 
        graphSize = atoi(argv[1]);
    }

    g = GRAPHinit(graphSize);    

    while (GRAPHedgeScan(&e)) {
        GRAPHinsertE(g, e);
    }

    edges = malloc(sizeof (*edges) * MAX_VERT * MAX_VERT);
    noOfEdges = GRAPHedges(edges, g);
    printf ("Edges of the graph:\n");
    for (i = 0; i < noOfEdges; i++) {
        GRAPHEdgePrint(edges[i]);
        printf("\n");
    }

#define test(a, b) printf("%2.1d %2.1d: %d\n", a, b, GRAPHpath(g, a, b));
    test(1, 2);
    test(9, 10);
    test(9, 5);
    test(4, 0);

    return EXIT_SUCCESS;
}
Пример #5
0
int dfs(void)
{
  Graph gr;
  Slist tasks;
  DfsVertexdata taskdata;
  int retval;
  SlistNode listnode;

  my_clearscrn();
  printf("--- TOPOLOGICAL SORTING/DFS DEMO ---");

  gr = GRAPHinit(dfs_match, dfs_destroy);

  /* Read task node(=vertex) data into graph.. */
  if ((read_tasknodes(gr, task_nodes, NR_OF_TASKNODES)) != OK)
    {
      fprintf(stderr, "Fatal error when reading task node data - bailing out..");
      GRAPHdestroy(gr);
      exit(-1);
    }
  
  /* Read task connection(=edge) data into graph.. */
  if ((read_taskconnections(gr, task_connections, NR_OF_TASKNODES)) != OK)
    {
      fprintf(stderr, "Fatal error when reading taskconnections data - bailing out..");
      GRAPHdestroy(gr);
      exit(-1);
    }

  prompt_and_pause("\n\nGraph created and graph data read..");
  printf("\nGRAPH:");

  /* Display graph.. */
  GRAPHprint(gr, prt_dfs_vtx, prt_dfs_edge);
  printf("\nNr of vertices/edges: %d/%d", GRAPHvcount(gr), GRAPHecount(gr));

  if ((retval = ALGOdfs(gr, &tasks)) != OK)
    {
      fprintf(stderr, "\nFatal error when calling 'ALGOdfs()'(Return value: %d) - Bailing out..", retval);
      GRAPHdestroy(gr);
      exit(-1);
    }

  printf("\n\nTopological Sort(DFS Analysis):\n-------------------------------");
  for (listnode = SLISThead(tasks); listnode != NULL; listnode = SLISTnext(listnode))
    {
      taskdata = (DfsVertexdata)SLISTdata(listnode);
      printf("\nNode %c, color=%d", *(char *)taskdata->data, taskdata->color);
    }

  prompt_and_pause("\n\nTime to tidy up..");

  SLISTdestroy(tasks);
  GRAPHdestroy(gr);

  return OK;
}
Пример #6
0
Graph GRAPHrand(int V, int E)
  { int i, j;
    double p = 2.0*E/V/(V-1);
    Graph G = GRAPHinit(V);
    for (i = 0; i < V; i++)
      for (j = 0; j < i; j++)
        if (rand() < p*RAND_MAX)
          GRAPHinsertE(G, EDGE(i, j));
    return G;
  }
Пример #7
0
Graph graphScan(char *file){
    int v,w;
    int V;
    FILE *in = fopen(file, "r");
    fscanf(in, "%d",&V);
    Graph g = GRAPHinit(V);
    while(fscanf(in,"%d %d",&v,&w) == 2){
        GRAPHinsertE(g,EDGE(v,w));
    }
    fclose(in);
    return g;
}
Пример #8
0
Graph GRAPHcopy(Graph g){

/* struct graph { int V; int E; vlink *adj; } *Graph; */
/* struct vnode { Vertex v; vlink next; } *vlink; */

    if(!g) return NULL; // exit if empty

    Graph bak = GRAPHinit(g->V);
    bak->E = g->E;

    int i = 0;

    while(i<=g->V){
        vlink node = (vlink)malloc(sizeof(vlink));
        node->v = g->adj[i]->v;
        bak->adj[i] = node;
        if(i!=0) bak->adj[i-1]->next = node;
        i++;
    }

    // TODO
    return bak;
}
Пример #9
0
/* --- Function: void basics(void) --- */
int basics(void)
{
  Graph gr;
  VertexNode vnode;
  EdgeNode enode;
  char data[STRSIZ], *pdata1, *pdata2;
  int retval;
  char mess[MESSIZ];

  /* Initialize the graph. */
  gr = GRAPHinit(bas_match, free);
  my_clearscrn();
  printf("--- BASIC GRAPH USAGE ---\n\nGraph created.. - let's do some basic graph editing..");
  /* --- Perform some graph operations --- */
  /* --- Start with inserting some vertices.. --- */

  /* Insert first vertex.. */
  if ((pdata1 = (char *)malloc(STRSIZ)) == NULL)
    return 1;
  strcpy(pdata1, "a");
  sprintf(mess, "\n\nInsert first vertex: %s", pdata1);
  prompt_and_pause(mess);
  if (GRAPHinsvertex(gr, pdata1) != 0) /* Do the insertion.. */
    return 1;
  /* Print the result.. */
  print_info(gr, prt_bas_vtx, prt_bas_edge);
  
  /* Insert next vertex.. */
  if ((pdata1 = (char *)malloc(STRSIZ)) == NULL)
    return 1;
  strcpy(pdata1, "b");
  sprintf(mess, "\n\nInsert vertex: %s", pdata1);
  prompt_and_pause(mess);
  if (GRAPHinsvertex(gr, pdata1) != 0) /* Do the insertion.. */
    return 1;
  /* Print the result.. */
  print_info(gr, prt_bas_vtx, prt_bas_edge);

  /* Insert next vertex.. */
  if ((pdata1 = (char *)malloc(STRSIZ)) == NULL)
    return 1;
  strcpy(pdata1, "c");
  sprintf(mess, "\n\nInsert vertex: %s", pdata1);
  prompt_and_pause(mess);
  if (GRAPHinsvertex(gr, pdata1) != 0) /* Do the insertion.. */
    return 1;
  /* Print the result.. */
  print_info(gr, prt_bas_vtx, prt_bas_edge);

  /* Insert next vertex.. */
  if ((pdata1 = (char *)malloc(STRSIZ)) == NULL)
    return 1;
  strcpy(pdata1, "d");
  sprintf(mess, "\n\nInsert vertex: %s", pdata1);
  prompt_and_pause(mess);
  if (GRAPHinsvertex(gr, pdata1) != 0) /* Do the insertion.. */
    return 1;
  /* Print the result.. */
  print_info(gr, prt_bas_vtx, prt_bas_edge);

  /* Insert next vertex.. */
  if ((pdata1 = (char *)malloc(STRSIZ)) == NULL)
    return 1;
  strcpy(pdata1, "e");
  sprintf(mess, "\n\nInsert vertex: %s", pdata1);
  prompt_and_pause(mess);
  if (GRAPHinsvertex(gr, pdata1) != 0) /* Do the insertion.. */
    return 1;
  /* Print the result.. */
  print_info(gr, prt_bas_vtx, prt_bas_edge);
  
  prompt_and_pause("\n\nCheck out all vertex insertions above..");

  /* --- Now, let's insert some edges.. --- */
  /* Insert first edge.. */
  if ((pdata2 = (char *)malloc(STRSIZ)) == NULL)
    return 1;
  strcpy(data, "a");
  strcpy(pdata2, "b");
  sprintf(mess, "\nNow - let's insert edge: %s --> %s", data, pdata2);
  prompt_and_pause(mess);
  if (GRAPHinsedge(gr, data, pdata2) != 0) /* Do the insertion.. */
    return 1;
  /* Print the result.. */
  print_info(gr, prt_bas_vtx, prt_bas_edge);

  /* Insert next edge.. */
  if ((pdata2 = (char *)malloc(STRSIZ)) == NULL)
    return 1;
  strcpy(data, "a");
  strcpy(pdata2, "c");
  sprintf(mess, "\n\nInsert edge: %s --> %s", data, pdata2);
  prompt_and_pause(mess);
  if (GRAPHinsedge(gr, data, pdata2) != 0)
    return 1;
  /* Print the result.. */
  print_info(gr, prt_bas_vtx, prt_bas_edge);

  /* Insert next edge.. */
  if ((pdata2 = (char *)malloc(STRSIZ)) == NULL)
    return 1;
  strcpy(data, "b");
  strcpy(pdata2, "c");
  sprintf(mess, "\n\nInsert edge: %s --> %s", data, pdata2);
  prompt_and_pause(mess);
  if (GRAPHinsedge(gr, data, pdata2) != 0) /* Do the insertion.. */
    return 1;
  /* Print the result.. */
  print_info(gr, prt_bas_vtx, prt_bas_edge);

  /* Insert next edge.. */
  if ((pdata2 = (char *)malloc(STRSIZ)) == NULL)
    return 1;
  strcpy(data, "b");
  strcpy(pdata2, "d");
  sprintf(mess, "\n\nInsert edge: %s --> %s", data, pdata2);
  prompt_and_pause(mess);
  if (GRAPHinsedge(gr, data, pdata2) != 0) /* Do the insertion.. */
    return 1;
  /* Print the result.. */
  print_info(gr, prt_bas_vtx, prt_bas_edge);

  /* Insert next edge.. */
  if ((pdata2 = (char *)malloc(STRSIZ)) == NULL)
    return 1;
  strcpy(data, "c");
  strcpy(pdata2, "b");
  sprintf(mess, "\n\nInsert edge: %s --> %s", data, pdata2);
  prompt_and_pause(mess);
  if (GRAPHinsedge(gr, data, pdata2) != 0) /* Do the insertion.. */
    return 1;
  /* Print the result.. */
  print_info(gr, prt_bas_vtx, prt_bas_edge);

  /* Insert next edge.. */
  if ((pdata2 = (char *)malloc(STRSIZ)) == NULL)
    return 1;
  strcpy(data, "c");
  strcpy(pdata2, "c");
  sprintf(mess, "\n\nInsert edge: %s --> %s", data, pdata2);
  prompt_and_pause(mess);
  if (GRAPHinsedge(gr, data, pdata2) != 0)/* Do the insertion.. */
    return 1;
  /* Print the result.. */
  print_info(gr, prt_bas_vtx, prt_bas_edge);

  /* Insert next edge.. */
  if ((pdata2 = (char *)malloc(STRSIZ)) == NULL)
    return 1;
  strcpy(data, "c");
  strcpy(pdata2, "d");
  sprintf(mess, "\n\nInsert edge: %s --> %s", data, pdata2);
  prompt_and_pause(mess);
  if (GRAPHinsedge(gr, data, pdata2) != 0) /* Do the insertion.. */
    return 1;
  /* Print the result.. */
  print_info(gr, prt_bas_vtx, prt_bas_edge);

  /* Insert next edge.. */
  if ((pdata2 = (char *)malloc(STRSIZ)) == NULL)
    return 1;
  strcpy(data, "d");
  strcpy(pdata2, "a");
  sprintf(mess, "\n\nInsert edge: %s --> %s", data, pdata2);
  prompt_and_pause(mess);
  if (GRAPHinsedge(gr, data, pdata2) != 0) /* Do the insertion.. */
    return 1;
  /* Print the result.. */
  print_info(gr, prt_bas_vtx, prt_bas_edge);

  /* Insert next edge.. */
  if ((pdata2 = (char *)malloc(STRSIZ)) == NULL)
    return 1;
  strcpy(data, "e");
  strcpy(pdata2, "c");
  sprintf(mess, "\n\nInsert edge: %s --> %s", data, pdata2);
  prompt_and_pause(mess);
  if (GRAPHinsedge(gr, data, pdata2) != 0) /* Do the insertion.. */
    return 1;
  /* Print the result.. */
  print_info(gr, prt_bas_vtx, prt_bas_edge);

  /* Insert next edge.. */
  if ((pdata2 = (char *)malloc(STRSIZ)) == NULL)
    return 1;
  strcpy(data, "e");
  strcpy(pdata2, "d");
  sprintf(mess, "\n\nInsert edge: %s --> %s", data, pdata2);
  prompt_and_pause(mess);
  if (GRAPHinsedge(gr, data, pdata2) != 0) /* Do the insertion.. */
    return 1;
  /* Print the result.. */
  print_info(gr, prt_bas_vtx, prt_bas_edge);

  /* Now, let's remove some edges.. */
  /* Remove first edge.. */
  if ((pdata2 = (char *)malloc(STRSIZ)) == NULL)
    return 1;
  strcpy(data, "a");
  strcpy(pdata2, "c");
  pdata1 = pdata2;
  sprintf(mess, "\n\nRemove edge %s --> %s", data, pdata2);
  prompt_and_pause(mess);
  if (GRAPHremedge(gr, data, (void **)&pdata1) != 0) /* Do the removal.. */
    {
      free(pdata2);
      return 1;
    }
  free(pdata1); /* Deallocate mem. held by removed edge.. */
  /* Print the result.. */
  print_info(gr, prt_bas_vtx, prt_bas_edge);

  /* Remove next edge.. */
  strcpy(data, "c");
  strcpy(pdata2, "c");
  pdata1 = pdata2;
  sprintf(mess, "\n\nRemove edge %s --> %s", data, pdata2);
  prompt_and_pause(mess);
  if (GRAPHremedge(gr, data, (void **)&pdata1) != 0)  /* Do the removal.. */
    {
      free(pdata2);
      return 1;
    }
  free(pdata1); /* Deallocate mem. held by removed edge.. */
  /* Print the result.. */
  print_info(gr, prt_bas_vtx, prt_bas_edge);

  /* Remove next edge.. */
  strcpy(data, "e");
  strcpy(pdata2, "c");
  pdata1 = pdata2;
  sprintf(mess, "\n\nRemove edge %s --> %s", data, pdata2);
  prompt_and_pause(mess);
  if (GRAPHremedge(gr, data, (void **)&pdata1) != 0)  /* Do the removal.. */
    {
      free(pdata2);
      return 1;
    }
  free(pdata1); /* Deallocate mem. held by removed edge.. */
  /* Print the result.. */
  print_info(gr, prt_bas_vtx, prt_bas_edge);

  /* Remove next edge.. */
  strcpy(data, "a");
  strcpy(pdata2, "b");
  pdata1 = pdata2;
  sprintf(mess, "\n\nRemove edge %s --> %s", data, pdata2);
  prompt_and_pause(mess);
  if (GRAPHremedge(gr, data, (void **)&pdata1) != 0)  /* Do the removal.. */
    {
      free(pdata2);
      return 1;
    }
  free(pdata1); /* Deallocate mem. held by removed edge.. */
  /* Print the result.. */
  print_info(gr, prt_bas_vtx, prt_bas_edge);

  /* Remove next edge.. */
  strcpy(data, "d");
  strcpy(pdata2, "a");
  pdata1 = pdata2;
  sprintf(mess, "\n\nRemove edge %s --> %s", data, pdata2);
  prompt_and_pause(mess);
  if (GRAPHremedge(gr, data, (void **)&pdata1) != 0)  /* Do the removal.. */
    {
      free(pdata2);
      return 1;
    }
  free(pdata1); /* Deallocate mem. held by removed edge.. */
  free(pdata2); /* Deallocate mem. held by search key value(=a).. */
  /* Print the result.. */
  print_info(gr, prt_bas_vtx, prt_bas_edge);

  /* Now, lets remove a vertex.. */
  strcpy(data, "a");
  pdata1 = data;
  sprintf(mess, "\n\nRemove vertex %s", data);
  prompt_and_pause(mess);
  if (GRAPHremvertex(gr, (void **)&pdata1) != 0)  /* Do the removal.. */
    return 1;
  free(pdata1); /* Deallocate mem. held by removed vertex.. */
  /* Print the result.. */
  print_info(gr, prt_bas_vtx, prt_bas_edge);

  /* --- Do some invalid insertions/removals --- */
  /* First invalid operation.. */
  if ((pdata2 = (char *)malloc(STRSIZ)) == NULL)
    return 1;
  strcpy(data, "f");
  strcpy(pdata2, "a");
  sprintf(mess,"\n\nNow - try to insert an invalid edge from %s --> %s", data, pdata2);
  prompt_and_pause(mess);
  retval = GRAPHinsedge(gr, data, pdata2); /* Do the insertion.. */
  if (retval != 0)
    free(pdata2); /* Deallocte mem. held by search key value(=a).. */
  /* Print the result.. */
  print_info(gr, prt_bas_vtx, prt_bas_edge);
  print_errmess(retval);

  /* Next invalid operation.. */
  if ((pdata2 = (char *)malloc(STRSIZ)) == NULL)
    return 1;
  strcpy(data, "c");
  strcpy(pdata2, "b");
  sprintf(mess,"\nInsert an existing edge from %s --> %s", data, pdata2);
  prompt_and_pause(mess);
  retval = GRAPHinsedge(gr, data, pdata2); /* Do the insertion.. */
  if (retval != 0)
    free(pdata2); /* Deallocte mem. held by search key value(=b).. */
  /* Print the result.. */
  print_info(gr, prt_bas_vtx, prt_bas_edge);
  print_errmess(retval);

  /* Next invalid operation.. */
  if ((pdata2 = (char *)malloc(STRSIZ)) == NULL)
    return 1;
  strcpy(data, "f");
  strcpy(pdata2, "a");
  pdata1 = pdata2;
  retval = GRAPHremedge(gr, data, (void **)&pdata2);  /* Do the removal.. */
  sprintf(mess, "\nRemove an invalid edge from %s --> %s", data, pdata2);
  prompt_and_pause(mess);
  if (retval == 0)
    {
      free(pdata2); /* Deallocte mem. held by removed edge.. */
      free(pdata1); /* Deallocte mem. held by search key value(=b).. */
      return 1;
    }
  free(pdata2); /* Deallocte mem. held by search key value(=b).. */
  /* Print the result.. */
  print_info(gr, prt_bas_vtx, prt_bas_edge);
  print_errmess(retval);

  /* Next invalid operation.. */
  if ((pdata2 = (char *)malloc(STRSIZ)) == NULL)
    return 1;
  strcpy(data, "c");
  strcpy(pdata2, "e");
  pdata1 = pdata2;
  retval = GRAPHremedge(gr, data, (void **)&pdata2);  /* Do the removal.. */
  sprintf(mess, "\nRemoving an invalid edge from %s --> %s", data, pdata2);
  prompt_and_pause(mess);
  if (retval == 0)
    {
      free(pdata2); /* Deallocte mem. held by removed edge.. */
      free(pdata1); /* Deallocte mem. held by search key value(=e).. */
      return 1;
    }
  free(pdata2); /* Deallocte mem. held by search key value(=e).. */
  /* Print the result.. */
  print_info(gr, prt_bas_vtx, prt_bas_edge);
  print_errmess(retval);

  /* Next invalid operation.. */
  if ((pdata2 = (char *)malloc(STRSIZ)) == NULL)
    return 1;
  strcpy(pdata2, "c");
  retval = GRAPHinsvertex(gr, pdata2);  /* Do the insertion.. */
  sprintf(mess, "\nInsert an existing vertex %s", data);
  prompt_and_pause(mess);
  if (retval != 0) /* Insertion failed - which is the case here.. */
    free(pdata2); /* Deallocte mem. held by search key value(=c).. */
  /* Print the result.. */
  print_info(gr, prt_bas_vtx, prt_bas_edge);
  print_errmess(retval);

  /* Next invalid operation.. */
  if ((pdata2 = (char *)malloc(STRSIZ)) == NULL)
    return 1;
  strcpy(pdata2, "c");
  pdata1 = pdata2;
  retval = GRAPHremvertex(gr, (void **)&pdata2);  /* Do the insertion.. */
  sprintf(mess, "\nRemove an existing vertex %s", pdata1);
  prompt_and_pause(mess);
  if (retval == 0)
    {
      free(pdata2); /* Deallocte mem. held by removed vertex.. */
      free(pdata1); /* Deallocte mem. held by search key value(=c).. */
      return 1;
    }
  free(pdata2); /* Deallocte mem. held by search key value(=c).. */
  /* Print the result.. */
  print_info(gr, prt_bas_vtx, prt_bas_edge);
  print_errmess(retval);

  /* Next invalid operation.. */
  if ((pdata2 = (char *)malloc(STRSIZ)) == NULL)
    return 1;
  strcpy(pdata2, "d");
  pdata1 = pdata2;
  retval = GRAPHremvertex(gr, (void **)&pdata2);  /* Do the insertion.. */
  sprintf(mess, "\nRemove an existing vertex %s", pdata1);
  prompt_and_pause(mess);
  if (retval == 0)
    {
      free(pdata2); /* Deallocte mem. held by removed vertex.. */
      free(pdata1); /* Deallocte mem. held by search key value(=d).. */
      return 1;
    }
  free(pdata2); /* Deallocte mem. held by search key value(=d).. */
  /* Print the result.. */
  print_info(gr, prt_bas_vtx, prt_bas_edge);
  print_errmess(retval);

  /* --- Testing GRAPHis_adjacent() --- */
  /* First test of GRAPHis_adjacent().. */
  if ((pdata2 = (char *)malloc(STRSIZ)) == NULL)
    return 1;
  strcpy(data, "b");
  strcpy(pdata2, "d");
  retval = GRAPHis_adjacent(gr, data, pdata2); /* Do the test.. */
  sprintf(mess, "\nTest if (%s, %s) are adjacent...", data, pdata2);
  prompt_and_pause(mess);
  /* Print the result.. */
  print_info(gr, prt_bas_vtx, prt_bas_edge);
  print_testmess(retval);

  /* Next test of GRAPHis_adjacent().. */
  strcpy(data, "a");
  strcpy(pdata2, "e");
  retval = GRAPHis_adjacent(gr, data, pdata2); /* Do the test.. */
  sprintf(mess, "\n\nTest if (%s, %s) are adjacent...", data, pdata2);
  prompt_and_pause(mess);
  /* Print the result.. */
  print_info(gr, prt_bas_vtx, prt_bas_edge);
  print_testmess(retval);

  /* Next test of GRAPHis_adjacent().. */
  strcpy(data, "e");
  strcpy(pdata2, "d");
  retval = GRAPHis_adjacent(gr, data, pdata2); /* Do the test.. */
  sprintf(mess, "\n\nTest if (%s, %s) are adjacent...", data, pdata2);
  prompt_and_pause(mess);
  /* Print the result.. */
  print_info(gr, prt_bas_vtx, prt_bas_edge);
  print_testmess(retval);

  /* Next test of GRAPHis_adjacent().. */
  strcpy(data, "c");
  strcpy(pdata2, "a");
  retval = GRAPHis_adjacent(gr, data, pdata2); /* Do the test.. */
  sprintf(mess, "\n\nTest if (%s, %s) are adjacent...", data, pdata2);
  prompt_and_pause(mess);
  /* Print the result.. */
  print_info(gr, prt_bas_vtx, prt_bas_edge);
  print_testmess(retval);

  /* Free temporary dyn. memory - no longer used.. */
  free(pdata2);

  /* Print all adjacent vertices - to a certain vertex.. */
  strcpy(data, "c");
  sprintf(mess, "\n\nFinally - print vertices adjacent to %s: ", data);
  prompt_and_pause(mess);

  vnode = GRAPHfindvertex(gr, data);
  printf("\nVertices adjacent(=incident) from %s (%d pcs): ", data, GRAPHgetedgecount(vnode));

  for (enode = GRAPHgetedgehead(vnode); enode != NULL; enode = GRAPHgetedgenext(enode))
    {
      printf("%s ", (char *)GRAPHgetedgedata(enode));
    }

  /* Destroy the graph. */
  sprintf(mess, "\n\nTime to tidy up..");
  prompt_and_pause(mess);

  GRAPHdestroy(gr);

  return OK;
}
Пример #10
0
int main (int argc, char * argv[]) {
    GRAPH graph;
    FILE * finput;
    symboltable table;
    EDGE tmpedge;
    ITEM * items;

    finput=fopen(argv[1], "r");
    int V;
    fscanf(finput, "%d", &V);

    graph=GRAPHinit(V);
    table=HASHinit(V);

    items=malloc(V*sizeof(ITEM));
    int i;
    for(i=0; i<V; i++) items[i]=NULL;

    i=0;
    char v1[10+1];
    char v2[10+1];
    while(fscanf(finput, "%s %s", v1, v2)==2) {
        if(HASHreturn(table, v1)==-1) {
            items[i]=ITEMinit(v1);
            HASHinsert(table, items[i], i);
            i++;
        }

        if(HASHreturn(table, v2)==-1) {
            items[i]=ITEMinit(v2);
            HASHinsert(table, items[i], i);
            i++;
        }
        tmpedge=EDGEadd(HASHreturn(table, v1), HASHreturn(table, v2));
        GRAPHinsertE(graph, tmpedge);
    }

    int * solution;
    int cmd, exit=0, path, v, w, j, k;
    while(!exit) {
        i=1;
        printf("\n");
        printf("%d Caclolare il camino semplice di lunghezza minima tra due vertici\n", i++);
        printf("%d Caclolare il camino semplice di lunghezza massima tra due vertici\n", i++);
        printf("%d Caclolare il numero di camini semplici tra due vertici\n", i++);
        printf("%d Calcolare le componenti fortemente connesse al grafo\n", i++);
        printf("%d Individuare un possibile sottoinsieme di archi per ottenere un grafo fortemente connesso\n", i++);
        printf("%d Esci\n--> ", i++);
        scanf("%d", &cmd);
        switch(cmd) {
        case 1:
            printf("Inserire il nome dei due nodi separati da spazio: ");
            scanf("%s %s", v1, v2);
            path=0;
            v=HASHreturn(table, v1);
            w=HASHreturn(table, v2);
            solution = optimalmin(graph, v, w, V);
            printf("Nodi attraversati:\n");
            ITEMshow(items[v], stdout);
            while(v!=w) {
                printf(" -> ");
                v=solution[v];
                ITEMshow(items[v], stdout);
                path++;
            }
            printf("\nLa lunghezza è %d\n", path);
            break;
        case 2:
            printf("Inserire il nome dei due nodi separati da spazio: ");
            scanf("%s %s", v1, v2);
            path=0;
            v=HASHreturn(table, v1);
            w=HASHreturn(table, v2);
            solution = optimalmax(graph, v, w, V);
            printf("Nodi attraversati:\n");
            ITEMshow(items[v], stdout);
            while(v!=w) {
                printf(" -> ");
                v=solution[v];
                ITEMshow(items[v], stdout);
                path++;
            }
            printf("\nLa lunghezza è %d\n", path);
            break;
        case 3:
            printf("Inserire il nome dei due nodi separati da spazio: ");
            scanf("%s %s", v1, v2);
            printf("Il numero dei cammini e' %d\n", npath(graph, HASHreturn(table, v1), HASHreturn(table, v2), V));
            break;
        case 4:
            k=0;
            for(i=0; i<V; i++)
                for(j=i+1; j<V; j++) {
                    if(GRAPHpath(graph, i, j) && GRAPHpath(graph, j, i)) {
                        k++;
                        ITEMshow(items[i], stdout);
                        printf(" e' fortemente connesso con ");
                        ITEMshow(items[j], stdout);
                        printf("\n");
                    }
                }
            printf("Ci sono %d componenti fortemente connesse\n", k);
            break;
        case 5:
            k=0;
            STACK additionaledges=STACKinit();
            for(i=0; i<V; i++) for(j=i+1; j<V; j++) {
                    int ab=GRAPHpath(graph, i, j);
                    int ba=GRAPHpath(graph, j, i);
                    if(!(ab && ba)) {
                        if(!ab) {
                            tmpedge = EDGEadd(i, j);
                            STACKpush(additionaledges, tmpedge);
                            GRAPHinsertE(graph, tmpedge);
                        }
                        if(!ba) {
                            tmpedge = EDGEadd(j, i);
                            STACKpush(additionaledges, tmpedge);
                            GRAPHinsertE(graph, tmpedge);
                        }
                    }
                }
            while(STACKcardinality(additionaledges)) {
                tmpedge=STACKpop(additionaledges);
                ITEMshow(items[tmpedge.v], stdout);
                printf(" -> ");
                ITEMshow(items[tmpedge.w], stdout);
                printf("\n");
            }
            break;
        case 6:
            exit=1;
        }
    }
    return 0;
}