Dijkstra::Dijkstra(Graph& g):graph(g){ VertexList(0).swap(nodelist); for (int i = 0; i < graph.vertices().size(); i++) { nodelist.push_back(graph[i]); } nodesize = graph.num_vertices(); }
Tree<Vertex*>* Graph::BFS(int vindex) { int queue[100]; //using an array as a queue int qtail=-1, qhead=0; int cur_index = -1; Vertex *adjV = NULL; Vertex *curV = VertexList(vindex); if(!curV) { printf("invalid root!\n"); return NULL; } BFS_tree_ = new Tree<Vertex*>(); /* set root */ curV->set_BFS_d(0); curV->set_BFS_p(NULL); printf("insert %d to the tree\n", curV->key()); BFS_tree_->Insert(NULL, curV); curV->set_visited(1); /* enqueue root */ Enqueue(queue,qtail,vindex); while((cur_index = Dequeue(queue, qhead, qtail)) >= 0) { curV = VertexList(cur_index); /* go through the adj list of curV */ Edge *tempE = curV->edges(); while(tempE) { adjV = tempE->connects(); if((adjV != NULL) && !(adjV->visited()) ) { adjV->set_BFS_d(curV->BFS_d()); adjV->set_BFS_p(curV); printf("insert %d to the tree, as child of %d\n", adjV->key(), curV->key()); BFS_tree_->Insert(curV, adjV); adjV->set_visited(1); Enqueue(queue, qtail, adjV->key()); } tempE = tempE->next(); adjV = NULL; } } return BFS_tree_; }
LLDependenciesBase::VertexList LLDependenciesBase::topo_sort(int vertices, const EdgeList& edges) const { // Construct a Boost Graph Library graph according to the constraints // we've collected. It seems as though we ought to be able to capture // the uniqueness of vertex keys using a setS of vertices with a // string property -- but I don't yet understand adjacency_list well // enough to get there. All the examples I've seen so far use integers // for vertices. // Define the Graph type. Use a vector for vertices so we can use the // default topological_sort vertex lookup by int index. Use a set for // edges because the same dependency may be stated twice: Node "a" may // specify that it must precede "b", while "b" may also state that it // must follow "a". typedef boost::adjacency_list<boost::setS, boost::vecS, boost::directedS, boost::no_property> Graph; // Instantiate the graph. Without vertex properties, we need say no // more about vertices than the total number. Graph g(edges.begin(), edges.end(), vertices); // topo sort typedef boost::graph_traits<Graph>::vertex_descriptor VertexDesc; typedef std::vector<VertexDesc> SortedList; SortedList sorted; // note that it throws not_a_dag if it finds a cycle try { boost::topological_sort(g, std::back_inserter(sorted)); } catch (const boost::not_a_dag& e) { // translate to the exception we define std::ostringstream out; out << "LLDependencies cycle: " << e.what() << '\n'; // Omit independent nodes: display only those that might contribute to // the cycle. describe(out, false); throw Cycle(out.str()); } // A peculiarity of boost::topological_sort() is that it emits results in // REVERSE topological order: to get the result you want, you must // traverse the SortedList using reverse iterators. return VertexList(sorted.rbegin(), sorted.rend()); }
void AdjList::add(unsigned int v) { _list.emplace(v, VertexList()); }
void Graph::Build() { if(!adj_list_) adj_list_ = new Vertex(0); for(int i=1; i<6; i++) { Vertex *temp = new Vertex(i); AddVertex(temp); } Vertex *cur = VertexList(0); cur->AddEdge(VertexList(1)); cur->AddEdge(VertexList(2)); cur = VertexList(1); cur->AddEdge(VertexList(0)); cur = VertexList(2); cur->AddEdge(VertexList(0)); cur->AddEdge(VertexList(3)); cur->AddEdge(VertexList(4)); cur = VertexList(3); cur->AddEdge(VertexList(2)); cur->AddEdge(VertexList(4)); cur->AddEdge(VertexList(5)); cur = VertexList(4); cur->AddEdge(VertexList(2)); cur->AddEdge(VertexList(3)); cur->AddEdge(VertexList(5)); cur = VertexList(5); cur->AddEdge(VertexList(3)); cur->AddEdge(VertexList(4)); }