コード例 #1
0
int main() {
  
  std::cout << "start" << std::endl;
  typedef DiGraphAdjMatrix<std::string, int> Graph;
  Graph g;
  Graph::VertexPtr s = g.AddVertex("s");
  Graph::VertexPtr v1 = g.AddVertex("v1");
  Graph::VertexPtr v2 = g.AddVertex("v2");
  Graph::VertexPtr v3 = g.AddVertex("v3");
  Graph::VertexPtr v4 = g.AddVertex("v4");
  Graph::VertexPtr v5 = g.AddVertex("v5");
  Graph::VertexPtr v6 = g.AddVertex("v6");
  Graph::VertexPtr v7 = g.AddVertex("v7");
  g.AddEdge(*s, *v1, 1);
  g.AddEdge(*s, *v2, 1);
  g.AddEdge(*s, *v3, 1);
  g.AddEdge(*v1, *v4, 1);
  g.AddEdge(*v1, *v5, 1);
  g.AddEdge(*v5, *v6, 1);
  g.AddEdge(*v6, *v7, 1);

  DepthFirstVertexIterator<std::string, int> dfi(g, *s);
  while (dfi.HasNext()) {
    std::cout << "v:" << dfi.Next()->GetValue() << std::endl;
  }
  
  return 0;
}
コード例 #2
0
        Graph GeneratorWHEEL::DoGenerate(list<int> parameter)
        {
            int n = parameter.front();
            Graph result;

            Vertex* center = *(result.AddVertex());
            Coordinates centercoords(2, 0.0f);
            center->SetCoordinates(centercoords);

            Vertex* prev = NULL;
            Vertex* first = NULL;
            for(int i = 0; i < n; i++)
            {
                Vertex* v = *(result.AddVertex());

                Coordinates coords(2);
                coords[0] = -5 * cos(i * (2 * M_PI / n) + M_PI/2);
                coords[1] =  5 * sin(i * (2 * M_PI / n) + M_PI/2);
                v->SetCoordinates(coords);

                if(prev != NULL)
                    result.AddEdge(prev, v);
                else
                    first = v;

                result.AddEdge(v, center);
                prev = v;
            }

            if(prev != NULL && first != NULL)
                result.AddEdge(prev, first);

            return result;
        }
コード例 #3
0
int main () {
	Graph myGraph;
	Vertex * root;
	cout << "Simply enter the input and press Ctrl + Z to send EOF command" << endl;
	for (string line; getline(std::cin, line);) {
        size_t found;
        if ((found = line.find(" ")) != string::npos) {
        	if (root != NULL) {
        		root = myGraph.AddVertex(line.substr(0,found));
        		myGraph.AddVertex(line.substr(found + 1, string::npos));
        	}
        	else {
        		myGraph.AddVertex(line.substr(0,found));
        		myGraph.AddVertex(line.substr(found + 1, string::npos));
        	}
        	myGraph.AddEdge(line.substr(0,found), line.substr(found + 1, string::npos));
        }
    }
    myGraph.PrintSolution();
	return 0;
}
コード例 #4
0
    Graph* BuildQueryGraph(const Graph* gQuery, const Subgraph& subg)
    {
        typedef typename Graph::VertexProp VertexProp;
        typedef typename Graph::EdgeType Edge;
        typedef typename Graph::DirectedEdgeType DirectedEdge;
        int num_vertices = subg.VertexCount();
        std::map<uint64_t, uint64_t> norm_id_map;

        Graph* g = new Graph(num_vertices);
   
        for (Subgraph::VertexIterator it = subg.BeginVertexSet();
                it != subg.EndVertexSet(); it++) {
            uint64_t u = *it;
            std::map<uint64_t, uint64_t>::iterator id_map_it = norm_id_map.find(u);
            if (id_map_it == norm_id_map.end()) {
                uint64_t mapped_u = norm_id_map.size();
                norm_id_map[u] = mapped_u;
                const VertexProp& u_prop = 
                        gQuery->GetVertexProperty(u);
                g->AddVertex(mapped_u, u_prop);
            }
        }

        g->AllocateAdjacencyLists();
 
        for (Subgraph::EdgeIterator it = subg.BeginEdgeSet(); 
                it != subg.EndEdgeSet(); it++) {
            const dir_edge_t& dir_e = *it;
            DirectedEdge dir_edge;
            dir_edge.s = norm_id_map[dir_e.s];
            dir_edge.t = norm_id_map[dir_e.t];
            int num_edges;
            const Edge* edges = gQuery->Neighbors(dir_e.s, num_edges);
            for (int i = 0; i < num_edges; i++) {
                if (edges[i].dst == dir_e.t) {
                    dir_edge.type = edges[i].type;
                    dir_edge.edge_data = edges[i].edge_data;
                    break;
                }
            }
            g->AddEdge(dir_edge);
        }

        return g;
    }
// Алгоритм Джонсона с использованием алгоритма Дейкстры на самоорганизующейся куче
void Djonson_SelfOrganizationHeap(int** dist, Graph ADJ, int num_vert)
{
    Graph AdditionalGraph = ADJ;

    AdditionalGraph.AddVertex(ADJ.GetNumOfVertex() + 1);

    int* dist_Bellman_Ford = new int[AdditionalGraph.GetNumOfVertex()];// Он будет содержать значения не больше нуля

    for(int i = 0; i < AdditionalGraph.GetNumOfVertex() - 1; i++)
        AdditionalGraph.AddEdge(AdditionalGraph.GetNumOfVertex() - 1, i, 0);

    // Применяем Беллмана-Форда для графа с наличием отрицательных весов ребер
    if(Bellman_Ford(dist_Bellman_Ford, AdditionalGraph, AdditionalGraph.GetNumOfVertex(), AdditionalGraph.GetNumOfVertex() - 1))
    {
        for(ADJ.Reset(); !ADJ.IsEnd(); ADJ.GoNext())// Делаем неотрицательные веса для алгоритма Дейкстры
        {
            ADJ.SetWeightOfEdge(ADJ.GetCurrVertex(),
                ADJ.GetCurrEdge().vertB,
                ADJ.GetCurrEdge().weight + dist_Bellman_Ford[ADJ.GetCurrVertex()] - dist_Bellman_Ford[ADJ.GetCurrEdge().vertB]);
        }

        for(int i = 0; i < ADJ.GetNumOfVertex(); i++)
        {
            Dijkstra_SelfOrganizationHeap(dist[i], ADJ, ADJ.GetNumOfVertex(), i);
            for(int j = 0; j < ADJ.GetNumOfVertex(); j++)
                dist[i][j] = dist[i][j] + dist_Bellman_Ford[j] - dist_Bellman_Ford[i];
        }

        for(ADJ.Reset(); !ADJ.IsEnd(); ADJ.GoNext())// Обратно меняем веса на предыдущие у графа ADJ
        {
            ADJ.SetWeightOfEdge(ADJ.GetCurrVertex(),
                ADJ.GetCurrEdge().vertB,
                ADJ.GetCurrEdge().weight + dist_Bellman_Ford[ADJ.GetCurrEdge().vertB ] - dist_Bellman_Ford[ADJ.GetCurrVertex()]);
        }
    }
    else
        cout << "Graph contains a cycle of negative weight" << endl;
}
コード例 #6
0
        Graph ImportFilterGML::Import(std::istream& is)
        {
            OpenGraphtheory::Import::GMLLexer l(is);
            GMLTreeNode* GMLTreeRoot = new GMLTreeNode;
            l.yylex(GMLTreeRoot);

            Graph result;
            map<int, Vertex*> VertexRegister;
            GMLValueNode* GraphNode = GetValue<GMLValueNode>(GMLTreeRoot, "GRAPH");
            if(GraphNode == NULL)
                throw "GML Document contains no top-level element \"graph\" with children";

            bool GraphDirected = false;
            GMLValueInt* directed = GetValue<GMLValueInt>(GraphNode->value, "DIRECTED");
            if(directed != NULL)
                GraphDirected = directed->value == 1;

            // Iterate Nodes
            for(list<pair<string, GMLValue*> >::iterator n = GraphNode->value->Children.begin(); n != GraphNode->value->Children.end(); n++)
            {
                string N = n->first;
                transform(N.begin(), N.end(), N.begin(), ::toupper);
                if(N != "NODE")
                    continue;
                GMLValueNode *NValue = dynamic_cast<GMLValueNode*>(n->second);
                if(NValue == NULL)
                    continue;

                GMLValueInt* id = GetValue<GMLValueInt>(NValue->value, "ID");
                if(id == NULL)
                    continue;
                if(VertexRegister.find(id->value) != VertexRegister.end())
                    throw "Multiple nodes with the same id";
                Vertex* v = *(result.AddVertex());
                VertexRegister[id->value] = v;

                GMLValueString* label = GetValue<GMLValueString>(NValue->value, "LABEL");
                if(label != NULL)
                    v->SetLabel(label->value);

                GMLValueNode* graphics = GetValue<GMLValueNode>(NValue->value, "GRAPHICS");
                if(graphics != NULL)
                {
                    vector<float> coordinates;

                    GMLValueFloat* xf = GetValue<GMLValueFloat>(graphics->value, "X");
                    GMLValueInt* xi = GetValue<GMLValueInt>(graphics->value, "X");
                    if(xf != NULL) coordinates.push_back(xf->value);
                    else if(xi != NULL) coordinates.push_back(xi->value);
                    else coordinates.push_back(0);

                    GMLValueFloat* yf = GetValue<GMLValueFloat>(graphics->value, "Y");
                    GMLValueInt* yi = GetValue<GMLValueInt>(graphics->value, "Y");
                    if(yf != NULL) coordinates.push_back(yf->value);
                    else if(yi != NULL) coordinates.push_back(yi->value);
                    else coordinates.push_back(0);

                    GMLValueFloat* zf = GetValue<GMLValueFloat>(graphics->value, "Z");
                    GMLValueInt* zi = GetValue<GMLValueInt>(graphics->value, "Z");
                    if(zf != NULL) coordinates.push_back(zf->value);
                    else if(zi != NULL) coordinates.push_back(zi->value);
                    // a third coordinate is not enforced

                    v->SetCoordinates(coordinates);

                    GMLValueFloat* wf = GetValue<GMLValueFloat>(graphics->value, "W");
                    GMLValueInt* wi = GetValue<GMLValueInt>(graphics->value, "W");
                    if(wf != NULL) v->SetWeight(wf->value);
                    else if(wi != NULL) v->SetWeight(wi->value);

                }

            }

            for(list<pair<string, GMLValue*> >::iterator e = GraphNode->value->Children.begin(); e != GraphNode->value->Children.end(); e++)
            {
                string E = e->first;
                transform(E.begin(), E.end(), E.begin(), ::toupper);
                if(E != "EDGE")
                    continue;
                GMLValueNode *EValue = dynamic_cast<GMLValueNode*>(e->second);
                if(EValue == NULL)
                    continue;

                GMLValueInt* source = GetValue<GMLValueInt>(EValue->value, "SOURCE");
                GMLValueInt* target = GetValue<GMLValueInt>(EValue->value, "TARGET");
                if(source == NULL || target == NULL)
                    continue;
                if(VertexRegister.find(source->value) == VertexRegister.end()
                || VertexRegister.find(target->value) == VertexRegister.end())
                    continue;

                bool Directed = GraphDirected;
                GMLValueNode* graphics = GetValue<GMLValueNode>(EValue->value, "GRAPHICS");
                if(graphics != NULL)
                {
                    GMLValueString* type = GetValue<GMLValueString>(graphics->value, "TYPE");
                    if(type != NULL)
                        Directed = type->value == "arc";
                }

                EdgeIterator ei;
                if(Directed)
                    ei = result.AddArc(VertexRegister[source->value], VertexRegister[target->value]);
                else
                    ei = result.AddEdge(VertexRegister[source->value], VertexRegister[target->value]);

                GMLValueString* label = GetValue<GMLValueString>(EValue->value, "LABEL");
                if(label != NULL)
                    (*ei)->SetLabel(label->value);
            }

            delete GMLTreeRoot;
            return result;
        }