Пример #1
0
/* --- Function: void print_info(Graph gr, void (*vtxprt)(const void *data), void (*edgprt)(const void *data)) --- */
void print_info(Graph gr, void (*vtxprt)(const void *data), void (*edgprt)(const void *data))
{
  my_clearscrn();
  printf("--- BASIC GRAPH OPERATIONS ---\n");
  GRAPHprint(gr, vtxprt, edgprt);
  printf("\n\nNr of vertices/edges: %d/%d ", GRAPHvcount(gr), GRAPHecount(gr));
}
Пример #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
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;
}
Пример #4
0
/********************************************************************
  MAIN
********************************************************************/
int main (int argc, char** argv)
{
    char cname[MAX_NAME];
    char input_line[MAX_LEN];
    int v,w;
    char* fname;
    Graph graph = NULL;
    EdgeWeight ew;
    TS computers = NULL;
    int nComputers = 0;
    int end = 0;

    FILE *fin = NULL;
    FILE *fout = NULL;

    printf("Laboratorio 12 - Esercizio 1\n\n");

    if (argc != 3){
      printf("Errore sul numero di argomenti passati al programma\n");
      printf("Usage:\n%s input_file n_nodi_max\n",argv[0]);
      exit(FAILURE);
    }

    fname = strdup (argv[1]);
    nComputers = atoi(argv[2]);

    /*/
    fname = strdup (INPUT_FILE);
    nComputers = MAX_NODES;
    */

    fin = fopen(fname, "r");
    if (fin == NULL)
    {
        printf("Errore di apertura file %s\n", fname);
        exit(FAILURE);
    }

    computers = TScrea(nComputers);
    if (computers == NULL)
        return FAILURE;

    graph = graph_read(fin, computers, nComputers);
    if (graph == NULL)
        return FAILURE;

    fclose(fin);

    while (!end)
    {
        printf("Operazioni disponibili:\n");
        printf("0 -> Uscita\n");
        printf("1 -> Verifica di connettivita'\n");
        printf("2 -> Connectivity enforcement\n");
        printf("3 -> Rimozione di un computer\n");
        printf("4 -> Inserzione di una connessione\n");
        printf("5 -> Rimozione di una connessione\n");
        printf("6 -> Identificazione connessioni critiche\n");
        printf("7 -> Visualizzazione della rete\n");
        printf("8 -> Stampa su file della rete\n");
        printf("Inserire numero operazione: ");

        scanf("%s", input_line);

        switch (input_line[0])
        {

        case '1':
            if( GRAPHconnCheck(graph) == 1)  //verifica connettivita
            {
                printf("\nVerifica della connettivita' superata\n\n");
            }
            else
            {
                printf("\nVerificata della connettivita' FALLITA\n\n");
            }
            break;

        case '2':
            if( GRAPHconnCheck(graph) == 1 )  //conn. enforcement
            {
                printf("\nErrore, il grafo e' gia' connesso\n\n");
                break;
            }
            graph_conn_enforce(graph, computers);
            break;

        case '3':
            printf("\nNome del computer da rimuovere: "); //rimozione computer
            scanf("%s",cname);
            v = TScerca(computers,cname);
            if (v < 0)
            {
                printf("Nodo non trovato\n\n");
                break;
            }
            GRAPHremoveV(graph,v);
            break;

        case '4':
            printf("\nNome del primo estremo della connessione da inserire: "); //inserzione connessione
            scanf("%s",cname);
            v = TScercaOAggiungi(computers,cname);
            if (v < 0)
            {
                printf("Impossibile inserire il nuovo nodo\n\n");
                break;
            }
            printf("Nome del seconde estremo della connessione da inserire: ");
            scanf("%s",cname);
            w = TScercaOAggiungi(computers,cname);
            if (w < 0)
            {
                printf("Impossibile inserire il nuovo nodo\n\n");
                break;
            }
            printf("Velocita' della connessione: ");
            scanf("%d", &ew.speed);
            if (ew.speed < 0)
            {
                printf("Errore, velocita' minore di 0\n\n");
                break;
            }
            GRAPHinsertE(graph,v,w,ew);
            break;

        case '5':
            printf("\nNome del primo estremo della connessione da rimuovere: "); //rimozione connessione
            scanf("%s",cname);
            v = TScerca(computers,cname);
            if (v < 0)
            {
                printf("Nodo non trovato\n\n");
                break;
            }
            printf("Nome del secondo estremo della connessione da rimuovere: ");
            scanf("%s",cname);
            w = TScerca(computers,cname);
            if (w < 0)
            {
                printf("Nodo non trovato\n\n");
                break;
            }
            GRAPHremoveE(graph,v,w);
            break;

        case '6':
            if( GRAPHconnCheck(graph) != 1 )  		//connessioni critiche
            {
                printf("\nErrore, il grafo non e' connesso\n\n");
                break;
            }
            graph_critic_conn(graph,computers);
            break;

        case '7':
            GRAPHshow(graph, computers);	//visualizzazione rete
            break;

        case '8':
            printf("\nNome del file in output: "); //stampa rete su file
            scanf("%s",cname);
            fout=fopen(cname,"w");
            if (fout == NULL)
            {
                printf("Errore di apertura file %s\n\n", fname);
                exit(FAILURE);
            }
            GRAPHprint(graph, computers, fout);
            fclose(fout);
            break;

        case '0':
            printf("\nProgramma terminato\n");
            end = 1;
            break;

        default:
            printf("Operazione sconosciuta\n\n");
        }
    }

    GRAPHfree(graph);
    TSlibera(computers);

    return SUCCESS;
}