Пример #1
0
int main(int argc, char* argv[]) {
  Adjacency_Matrix* g = (Adjacency_Matrix*) malloc(sizeof(Adjacency_Matrix));
  Adjacency_Matrix* tp;
  int* adjacency;
  empty_graph(g);

  int option, v, a1, a2;

  do {
    menu();
    scanf("%d", &option);
    switch (option) {
      case INSERT_VERTEX:
        printf("How many vertex would you like to insert? ");
        scanf("%d", &v);
        insert_vertex(g, v);
        break;
      case REMOVE_VERTEX:
        printf("Which vertex would you like to remove? ");
        scanf("%d", &v);
        remove_vertex(g, v);
        break;
      case INSERT_ARC:
        printf("First vertex: ");
        scanf("%d", &a1);
        printf("Second vertex: ");
        scanf("%d", &a2);
        insert_arc(g, a1, a2, 1);
        break;
      case REMOVE_ARC:
        printf("First vertex: ");
        scanf("%d", &a1);
        printf("Second vertex: ");
        scanf("%d", &a2);
        remove_arc(g, a1, a2);
        break;
      case VERTEX_ADJACENCY:
        printf("Which vertex would you like to verify adjacency?");
        scanf("%d", &v);
        adjacency = get_adjacency(g, v);
        print_adjacency(adjacency);
        free(adjacency);
        pause();
        break;
      case TRANSPOSE_GRAPH:
        tp = transpose_graph(g);
        print_graph(tp);
        free(tp);
        pause();
        break;
      case PRINT_GRAPH: 
        print_graph(g);
        pause();
        break;
    }
  } while (option != EXIT);

  return 0;
}
Пример #2
0
/*
 * This happend to be our first approuch to get the maximum matching.
 * THe results are pretty satisfatory and the code complexity isnt that bad at all
 */
Graph* maximal_matching(Graph* g) {
  Graph* m = (Graph*) malloc(sizeof(Graph));
  Graph* matching = (Graph*) malloc(sizeof(Graph));

  init_graph(m, g->vertex_count);
  init_graph(matching, g->vertex_count); 

  int i;
  int j;

  // Make a copy of the graph ignoring loops
  for (i = 0 ; i < g->vertex_count ; i++)
    for (j = 0 ; j < g->vertex_count ; j++)
      if (i != j)
        m->arcs[i][j] = g->arcs[i][j];

  Vertex* v = (Vertex*) malloc(g->vertex_count * sizeof(Vertex));
  get_ordered_vertex(m, v);
  
  int saturated[m->vertex_count];
  for (i = 0 ; i < m->vertex_count ; i++)
    saturated[i] = 0;

  for (i = 0 ; i < m->vertex_count ; i++) {
    m->vertex[i].degree = get_vertex_degree(m, i);
  }

  int* adj;
  for (i = 0 ; i < m->vertex_count ; i++) {
    int v1 = v[i].vertex;
    int v2;

    if (saturated[v1] == 0) {
      adj = get_adjacency(m, v1);
      if (adj[0] > 0) {
        v2 = adj[1];
        
        for (j = 2 ; j <= adj[0] ; j++) {
          if (m->vertex[adj[j]].degree < m->vertex[v2].degree) 
            v2 = adj[j];
        }
        
        insert_arc(matching, v1, v2, m->arcs[v1][v2]);
        free(adj);
        saturated[v1] = 1;
        saturated[v2] = 1;
        
        for (j = 0 ; j < m->vertex_count ; j++) {
          remove_arc(m, v1, j);
          remove_arc(m, v2, j);
        }
      }
    }
  }
  
  return matching;
}
Пример #3
0
 value_type operator()(const int_type& number) const {
   return get_adjacency (number-offset);
 }