コード例 #1
0
ファイル: graph.c プロジェクト: vitorfs/graph
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
ファイル: strong_components.hpp プロジェクト: Aantonb/gotham
  typename property_traits<ComponentsMap>::value_type
  kosaraju_strong_components(Graph& G, ComponentsMap c,
                             FinishTime finish_time, ColorMap color)
  {
    function_requires< MutableGraphConcept<Graph> >();
    // ...
    
    typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
    typedef typename property_traits<ColorMap>::value_type ColorValue;
    typedef color_traits<ColorValue> Color;
    typename property_traits<FinishTime>::value_type time = 0;
    depth_first_search
     (G, make_dfs_visitor(stamp_times(finish_time, time, on_finish_vertex())),
      color);

    Graph G_T(num_vertices(G));
    transpose_graph(G, G_T);

    typedef typename property_traits<ComponentsMap>::value_type count_type;

    count_type c_count(0);
    detail::components_recorder<ComponentsMap>
      vis(c, c_count);

    // initialize G_T
    typename graph_traits<Graph>::vertex_iterator ui, ui_end;
    for (tie(ui, ui_end) = vertices(G_T); ui != ui_end; ++ui)
      put(color, *ui, Color::white());

    typedef typename property_traits<FinishTime>::value_type D;
    typedef indirect_cmp< FinishTime, std::less<D> > Compare;

    Compare fl(finish_time);
    std::priority_queue<Vertex, std::vector<Vertex>, Compare > Q(fl);

    typename graph_traits<Graph>::vertex_iterator i, j, iend, jend;
    tie(i, iend) = vertices(G_T);
    tie(j, jend) = vertices(G);
    for ( ; i != iend; ++i, ++j) {
      put(finish_time, *i, get(finish_time, *j));
       Q.push(*i);
    }

    while ( !Q.empty() ) {
      Vertex u = Q.top();
      Q.pop();
      if  (get(color, u) == Color::white()) {
        depth_first_visit(G_T, u, vis, color);
        ++c_count; 
      }
    }
    return c_count;
  }
コード例 #3
0
ファイル: connected_components.hpp プロジェクト: rtsuk/fmsync
    inline typename property_traits<Components>::value_type
    connected_components(Graph& G, DFSVisitor v, Components c, DiscoverTime d,
                         FinishTime f, Color color, directed_tag)
    {
      typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
      typename property_traits<Color>::value_type cc = get(color, Vertex());
      int time = 0;
      depth_first_search(G, record_times(d, f, time, v), color);

      Graph G_T(num_vertices(G));
      transpose_graph(G, G_T);

      typedef typename property_traits<Components>::value_type count_type;

      count_type c_count(0);
      components_recorder<Components, dfs_visitor<> > 
        vis(c, c_count, dfs_visitor<>());

      // initialize G_T
      typename graph_traits<Graph>::vertex_iterator ui, ui_end;
      for (tie(ui, ui_end) = vertices(G_T); ui != ui_end; ++ui)
        put(color, *ui, white(cc));

      typedef typename property_traits<FinishTime>::value_type D;
      typedef indirect_cmp< FinishTime, std::less<D> > Compare;

      Compare fl(f);
      priority_queue<Vertex, std::vector<Vertex>, Compare > Q(fl);

      typename graph_traits<Graph>::vertex_iterator i, j, iend, jend;
      tie(i, iend) = vertices(G_T);
      tie(j, jend) = vertices(G);
      for ( ; i != iend; ++i, ++j) {
        put(f, *i, get(f, *j));
         Q.push(*i);
      }

      while ( !Q.empty() ) {
        Vertex u = Q.top();
        Q.pop();
        if  (get(color, u) == white(cc)) {
          depth_first_visit(G_T, u, vis, color);
          ++c_count; 
        }
      }
      return c_count;
    }