Beispiel #1
0
int main() {
    scanf("%d%d", &N, &M);
    Graph g(N, M);
    for (int i = 0; i < M; i++) {
        int x, y;
        scanf("%d%d", &x, &y);
        x--; y--;
        g.addEdge(x, y);
    }

    TopologicalSort topo;
    topo.sort(g);

    vector<int> ans(N, 1);
    for (int x: topo.topo) {
        for (int eidx = g.head[x]; ~eidx; eidx = g.E[eidx].next) {
            int v = g.E[eidx].to;
            ans[v] = max(ans[v], 1 + ans[x]);
        }
    }
    for (int x: ans) {
        printf("%d\n", x);
    }
    return 0;
}
Beispiel #2
0
int main(int argc, const char * argv[]) {
    if(argc != 2)
    {
        cout << "Arguments invalides" << endl;
    }
    else
    {
        SymbolGraph<DiGraph> graph = SymbolGraph<DiGraph>(argv[1]);
        TopologicalSort<DiGraph> tps = TopologicalSort<DiGraph>(graph.G());
        DirectedCycle<DiGraph> directedCycle = DirectedCycle<DiGraph>(graph.G());
        // Si on a pas de cycle on peut trier !
        if(directedCycle.HasCycle() == false)
        {
            auto order = tps.Order();
            cout << "est un Directed Cycle " << endl;
            
            if(checkOrder(tps.Order(), graph, argv[1], ','))
            {
                for(auto index: order)
                {
                    cout << graph.name(index) << endl;
                }
                cout << "Verification reussie" << endl;
            }
            else
            {
                cout << argv[1] << "nest pas un DAG" << endl << endl;
                cout << "Cycle trouve:" << endl;
                for(auto index: directedCycle.Cycle())
                {
                    cout << graph.name(index);
                }
                cout << "Verification echouee" << endl;
            }
        }
    }
    /* A IMPLEMENTER */
    
    return EXIT_SUCCESS;
} 
Beispiel #3
0
int main()
{
	Graph *graph = Graph::create(9);

	graph_info info;
    info.source = 0;
    info.destination = 0;

    graph->addEdge(0, 1, 4);
    graph->addEdge(0, 7, 8);
    graph->addEdge(1, 2, 8);
    graph->addEdge(1, 7, 11);
    graph->addEdge(2, 3, 7);
    graph->addEdge(2, 8, 2);
    graph->addEdge(2, 5, 4);
    graph->addEdge(3, 4, 9);
    graph->addEdge(3, 5, 14);
    graph->addEdge(4, 5, 10);
    graph->addEdge(5, 6, 2);
    graph->addEdge(6, 7, 1);
    graph->addEdge(6, 8, 6);
    graph->addEdge(7, 8, 7);

    // DFS
    LOG("======== DFS start========");
    DFS *dfs = DFS::create(graph, info);
    dfs->compute();
    delete(dfs);
    LOG("======== DFS end========");

    // BFS
    LOG("======== BFS start========");
    BFS *bfs = BFS::create(graph, info);
    bfs->compute();
    delete(bfs);
    LOG("======== BFS end========");

    // dijkstra
    LOG("======== dijkstra start========");
    dijkstra *dijkstra = dijkstra::create(graph, info);
    dijkstra->compute();
    delete(bfs);
    LOG("======== dijkstra end========");

    // PrimMST
    LOG("======== PrimMST start========");
    PrimMST *prim = PrimMST::create(graph, info);
    prim->compute();
    delete(prim);
    LOG("======== PrimMST end========");

    // BellmanFord
    LOG("======== BellmanFord start========");
    BellmanFord *bellman_ford = BellmanFord::create(graph, info);
    bellman_ford->compute();
    delete(bellman_ford);
    LOG("======== BellmanFord end========");

    // ShortestPath
    LOG("======== ShortestPath start========");
    ShortestPath *shortest_path = ShortestPath::create(graph, info);
    shortest_path->compute();
    delete(shortest_path);
    LOG("======== ShortestPath end========");

    // Detect Cycle
    LOG("======== Detect Cycle start========");
    isCyclic(graph, info);
    LOG("======== Detect Cycle end========");

    // Detect Cycle using Disjoint set
    LOG("======== Detect Cycle using Disjoint set start========");
    LOG(isCyclicUsingDisjointSet(graph, info));
    LOG("======== Detect Cycle using Disjoint set end========");

    // Graph Coloring
    LOG("======== Graph Coloring start========");
    GraphColoring *graph_color = GraphColoring::create(graph, info);
    graph_color->color();
    graph_color->print();
    LOG("======== Graph Coloring end========");

    // Hamilton Cycle
    LOG("======== Hamilton Cycle start========");
    HamiltonCycle *hamiton = HamiltonCycle::create(graph, info);
    hamiton->checkHamiltonCycle();
    LOG("======== Hamilton Cycle end========");

    delete graph;

    // Directed Graph
    graph = Graph::create(6);

    graph->addDirectedEdge(0, 1);
    graph->addDirectedEdge(0, 5);
    graph->addDirectedEdge(1, 2);
    graph->addDirectedEdge(1, 3);
    graph->addDirectedEdge(1, 4);
    graph->addDirectedEdge(2, 3);
    graph->addDirectedEdge(2, 4);
    graph->addDirectedEdge(3, 4);
    graph->addDirectedEdge(5, 2);

    LOG("======== Topological Sort start========");
    TopologicalSort *tsort = TopologicalSort::create(graph, info);
    tsort->sort();
    tsort->print();
    LOG("======== Topological Sort end========");

	return 0;
}
Beispiel #4
0
MultSeqAlignment *AlignmentBuilder::buildAlignment(bool includeAncestors,
						   bool useParsimony)
{
  // First, build a dependency graph indicating which connected 
  // components (represented by their residue roots) must come before 
  // others in the alignment:

  //if(useParsimony) {cout<<"L1"<<endl;dumpSeqLengths();}
  Poset<ResidueAddress> poset;
  //cout<<"build poset"<<endl;
  buildPoset(poset);

  //if(useParsimony) {cout<<"L2"<<endl;dumpSeqLengths();}
  //cout<<"sort dep graph"<<endl;
  // Sort the dependency graph to get a total ordering:
  TopologicalSort<ResidueAddress> sorter;
  typedef typename Poset<ResidueAddress>::Vertex<ResidueAddress> Node;
  //cout<<"sorter.sort"<<endl;
  Array1D<Node*> &sorted=*sorter.sort(poset);
  //cout<<"get align len"<<endl;
  int numColumns=getAlignmentLength(sorted);
  int numRoots=sorted.size();

  //if(useParsimony) {cout<<"L3"<<endl;dumpSeqLengths();}
  //cout<<"init empty align "<<numColumns<<Endl;
  // Initialize an empty alignment of the proper size
  MultSeqAlignment *msa=new MultSeqAlignment(alphabet,gapSymbol);
  Set<Taxon*>::iterator cur=taxa.begin(), end=taxa.end();
  for(; cur!=end ; ++cur) {
    Taxon *taxon=*cur;
    if(taxon->isLeaf() || includeAncestors) {
      AlignmentSeq &track=msa->findOrCreateTrack(taxon->getName());
      String &annoTrack=track.getAnnoTrack();
      annoTrack.padOrTruncate(numColumns);
    }
  }
  //if(useParsimony) {cout<<"L4"<<endl;dumpSeqLengths();}
  msa->extendToLength(numColumns);
  //if(useParsimony) {cout<<"L5"<<endl;dumpSeqLengths();}
  //cout<<"iterate total ord"<<Endl;
  //cout<<"BEFORE"<<endl; dumpSeqLengths();
  // Iterate across the total ordering, appending a new alignment column
  // for each element:
  if(useParsimony) {
    struct Visitor : public TreeVisitor {
      Symbol gapSymbol;
      Visitor(Symbol gap) : gapSymbol(gap) {}
      void processNode(InternalNode &v) {
	Taxon &taxon=*static_cast<Taxon*>(v.getDecoration());
	//cout<<"extending "<<taxon.getName()<<" "<<taxon.getSeqLen()<<endl;
	taxon.getSeq()=Sequence(gapSymbol,taxon.getSeqLen());
      }
      //void processNode(LeafNode &v) {}
      //void processNode(RootNode &v) {}
    } V(gapSymbol);
    root->preorderTraversal(V);
  }
  //cout<<"AFTER"<<endl;  dumpSeqLengths();
  //cout<<"last loop"<<endl;
  int col=0;
  for(int i=0 ; i<numRoots /*numColumns*/ ; ++i) {
    ResidueAddress addr=sorted[i]->getData();
    //cout<<"root "<<addr<<endl;
    if(!addr.hasLeafDescendents() && !wantOrphans) continue; // ### 
    //cout<<i<<" "<<useParsimony<<" "<<alphabet.size()<<endl;
    if(useParsimony) parsimony(addr);
    addColumn(col++,*msa,addr,includeAncestors);//NOT THE PROBLEM
    //cout<<"INDEX "<<i<<endl;
    //dumpSeqLengths();
  }
  //if(useParsimony) INTERNAL_ERROR; // DEBUGGING
  //cout<<"done last loop"<<endl;
  // Clean up
  delete &sorted;
  return msa;
}