Подтвердить что ты не робот

Выполнение графика С++

Мне было интересно быстро написать реализацию графика в С++. Мне нужна структура данных, чтобы легко манипулировать и использовать алгоритмы графа (такие как BFS, DFS, Kruskal, Dijkstra...). Мне нужна эта реализация для олимпиады по алгоритмам, поэтому проще написать структуру данных.

Можете ли вы предложить такие DS (основные структуры или классы и то, что будет в них). Я знаю, что главные возможности имеют список Adjacency и Adjacency, но я имею в виду более подробный код.

Например, я подумал об этом DS в прошлый раз, когда мне пришлось реализовать график для DFS:

struct Edge {
  int start;
  int end;
  struct Edge* nextEdge;
}

а затем использовал массив размером n, содержащий в своем i-м месте список границ (struct Edge), представляющий ребра, начиная с i'th node.

но при попытке DFS на этом графике мне пришлось написать 50-строчный код с примерно 10 циклами.

Какие "хорошие" реализации существуют?

4b9b3361

Ответ 1

Это действительно зависит от того, какие алгоритмы вам нужно реализовать, серебряной пули нет (и это не должно вызывать удивления... общее правило программирования - отсутствие общего правила ;-)).

Я часто заканчиваю тем, что представляю направленные мультиграфы, используя структуры узлов/ребер с указателями... более конкретно:

struct Node
{
    ... payload ...
    Link *first_in, *last_in, *first_out, *last_out;
};

struct Link
{
    ... payload ...
    Node *from, *to;
    Link *prev_same_from, *next_same_from,
         *prev_same_to, *next_same_to;
};

Другими словами, каждый узел имеет двусвязный список входящих ссылок и двусвязный список исходящих ссылок. Каждое звено знает from и to узлов и в то же время в двух различных двунаправленных списках: список всех ссылок, выходящем из тех же from узла и списка всех ссылок, прибывающих в том же to узлу.

Указатели prev_same_from и next_same_from используются при следовании цепи всех звеньев, выходящих из одного узла; указатели prev_same_to и next_same_to вместо этого используются при управлении цепочкой всех ссылок, указывающих на один и тот же узел.

Data structure diagram

В нем много путаницы с указателями (поэтому, если вы не любите указатели, просто забудьте об этом), но операции запроса и обновления эффективны; например, добавление узла или ссылки - это O (1), удаление ссылки - это O (1) и удаление узла x - это O (deg (x)).

Конечно, в зависимости от проблемы, размера полезной нагрузки, размера графа, плотности графа этот подход может быть слишком избыточным или слишком требовательным к памяти (в дополнение к полезной нагрузке у вас есть 4 указателя на узел и 6 указателей на ссылку).

Подобную структуру полной реализации можно найти здесь.

Ответ 2

Ниже приведена реализация структуры данных графа в С++ в виде списка привязанностей.

Я использовал вектор STL для представления вершин и пары STL для обозначения ребер и конечной вершины.

#include <iostream>
#include <vector>
#include <map>
#include <string>

using namespace std;

struct vertex {
    typedef pair<int, vertex*> ve;
    vector<ve> adj; //cost of edge, destination vertex
    string name;
    vertex(string s) : name(s) {}
};

class graph
{
public:
    typedef map<string, vertex *> vmap;
    vmap work;
    void addvertex(const string&);
    void addedge(const string& from, const string& to, double cost);
};

void graph::addvertex(const string &name)
{
    vmap::iterator itr = work.find(name);
    if (itr == work.end())
    {
        vertex *v;
        v = new vertex(name);
        work[name] = v;
        return;
    }
    cout << "\nVertex already exists!";
}

void graph::addedge(const string& from, const string& to, double cost)
{
    vertex *f = (work.find(from)->second);
    vertex *t = (work.find(to)->second);
    pair<int, vertex *> edge = make_pair(cost, t);
    f->adj.push_back(edge);
}

Ответ 3

Наиболее распространенными представлениями, вероятно, являются следующие два:

Из этих двух матриц смежности является самым простым, если вы не против иметь (возможно, огромный) массив n * n, где n - число вершин. В зависимости от базового типа массива вы можете даже сохранить весы краев для использования, например. алгоритмы обнаружения кратчайшего пути.

Ответ 4

Этот вопрос древний, но почему-то я не могу выбросить его из головы.

Хотя все решения обеспечивают реализацию графов, они также все очень многословны. Они просто не элегантны.

Вместо того, чтобы придумывать собственный класс графов, все, что вам действительно нужно, - это способ сказать, что одна точка связана с другой - для этого std::map и std::unordered_map отлично работают. Просто определите граф как карту между узлами и списками ребер. Если вам не нужны дополнительные данные на краю, список конечных узлов будет хорошо.

Таким образом, краткий граф в C++ может быть реализован так:

using graph = std::map<int, std::vector<int>>;

Или, если вам нужны дополнительные данные,

struct edge {
    int nodes[2];
    float cost; // add more if you need it
};

using graph = std::map<int, std::vector<edge>>;

Теперь ваша структура графа будет хорошо вписываться в остальную часть языка, и вам не нужно будет запоминать какой-либо новый неуклюжий интерфейс - старый неуклюжий интерфейс подойдет просто отлично.

Никаких ориентиров, но я чувствую, что это также превзойдет другие предложения здесь.

NB: int - это не индексы, а идентификаторы.

Ответ 5

Я предпочитаю использовать список смежности Индексы (не указатели)

typedef std::vector< Vertex > Vertices;
typedef std::set <int> Neighbours;


struct Vertex {
private:
   int data;
public:
   Neighbours neighbours;

   Vertex( int d ): data(d) {}
   Vertex( ): data(-1) {}

   bool operator<( const Vertex& ref ) const {
      return ( ref.data < data );
   }
   bool operator==( const Vertex& ref ) const {
      return ( ref.data == data );
   }
};

class Graph
{
private :
   Vertices vertices;
}

void Graph::addEdgeIndices ( int index1, int index2 ) {
  vertices[ index1 ].neighbours.insert( index2 );
}


Vertices::iterator Graph::findVertexIndex( int val, bool& res )
{
   std::vector<Vertex>::iterator it;
   Vertex v(val);
   it = std::find( vertices.begin(), vertices.end(), v );
   if (it != vertices.end()){
        res = true;
       return it;
   } else {
       res = false;
       return vertices.end();
   }
}

void Graph::addEdge ( int n1, int n2 ) {

   bool foundNet1 = false, foundNet2 = false;
   Vertices::iterator vit1 = findVertexIndex( n1, foundNet1 );
   int node1Index = -1, node2Index = -1;
   if ( !foundNet1 ) {
      Vertex v1( n1 );
      vertices.push_back( v1 );
      node1Index = vertices.size() - 1;
   } else {
      node1Index = vit1 - vertices.begin();
   }
   Vertices::iterator vit2 = findVertexIndex( n2, foundNet2);
   if ( !foundNet2 ) {
      Vertex v2( n2 );
      vertices.push_back( v2 );
      node2Index = vertices.size() - 1;
   } else {
      node2Index = vit2 - vertices.begin();
   }

   assert( ( node1Index > -1 ) && ( node1Index <  vertices.size()));
   assert( ( node2Index > -1 ) && ( node2Index <  vertices.size()));

   addEdgeIndices( node1Index, node2Index );
}

Ответ 6

Может быть даже более простое представление, предполагающее, что нужно только проверять алгоритмы графа, чтобы они не использовали их (график) где-то еще. Это может быть как карта из вершин в их списки смежности, как показано ниже: -

#include<bits/stdc++.h>
using namespace std;

/* implement the graph as a map from the integer index as a key to the   adjacency list
 * of the graph implemented as a vector being the value of each individual key. The
 * program will be given a matrix of numbers, the first element of each row will
 * represent the head of the adjacency list and the rest of the elements will be the
 * list of that element in the graph.
*/

typedef map<int, vector<int> > graphType;

int main(){

graphType graph;
int vertices = 0;

cout << "Please enter the number of vertices in the graph :- " << endl;
cin >> vertices;
if(vertices <= 0){
    cout << "The number of vertices in the graph can't be less than or equal to 0." << endl;
    exit(0);
}

cout << "Please enter the elements of the graph, as an adjacency list, one row after another. " << endl;
for(int i = 0; i <= vertices; i++){

    vector<int> adjList;                    //the vector corresponding to the adjacency list of each vertex

    int key = -1, listValue = -1;
    string listString;
    getline(cin, listString);
    if(i != 0){
        istringstream iss(listString);
        iss >> key;
        iss >> listValue;
        if(listValue != -1){
            adjList.push_back(listValue);
            for(; iss >> listValue; ){
                adjList.push_back(listValue);
            }
            graph.insert(graphType::value_type(key, adjList));
        }
        else
            graph.insert(graphType::value_type(key, adjList));
    }
}

//print the elements of the graph
cout << "The graph that you entered :- " << endl;
for(graphType::const_iterator iterator = graph.begin(); iterator != graph.end(); ++iterator){
    cout << "Key : " << iterator->first << ", values : ";

    vector<int>::const_iterator vectBegIter = iterator->second.begin();
    vector<int>::const_iterator vectEndIter = iterator->second.end();
    for(; vectBegIter != vectEndIter; ++vectBegIter){
        cout << *(vectBegIter) << ", ";
    }
    cout << endl;
}
}

Ответ 7

Вот базовая реализация графика. Примечание. Я использую вершину, которая привязана к следующей вершине. И каждая вершина имеет список, указывающий на соседние узлы.

#include <iostream>
using namespace std;


// 1 ->2 
// 1->4
// 2 ->3
// 4->3
// 4 -> 5
// Adjacency list
// 1->2->3-null
// 2->3->null
//4->5->null;

// Structure of a vertex
struct vertex {
   int i;
   struct node *list;
   struct vertex *next;
};
typedef struct vertex * VPTR;

// Struct of adjacency list
struct node {
    struct vertex * n;
    struct node *next;
};

typedef struct node * NODEPTR;

class Graph {
    public:
        // list of nodes chained together
        VPTR V;
        Graph() {
            V = NULL;
        }
        void addEdge(int, int);
        VPTR  addVertex(int);
        VPTR existVertex(int i);
        void listVertex();
};

// If vertex exist, it returns its pointer else returns NULL
VPTR Graph::existVertex(int i) {
    VPTR temp  = V;
    while(temp != NULL) {
        if(temp->i == i) {
            return temp;
        }
        temp = temp->next;
    }
   return NULL;
}
// Add a new vertex to the end of the vertex list
VPTR Graph::addVertex(int i) {
    VPTR temp = new(struct vertex);
    temp->list = NULL;
    temp->i = i;
    temp->next = NULL;

    VPTR *curr = &V;
    while(*curr) {
        curr = &(*curr)->next;
    }
    *curr = temp;
    return temp;
}

// Add a node from vertex i to j. 
// first check if i and j exists. If not first add the vertex
// and then add entry of j into adjacency list of i
void Graph::addEdge(int i, int j) {

    VPTR v_i = existVertex(i);   
    VPTR v_j = existVertex(j);   
    if(v_i == NULL) {
        v_i = addVertex(i);
    }
    if(v_j == NULL) {
        v_j = addVertex(j);
    }

    NODEPTR *temp = &(v_i->list);
    while(*temp) {
        temp = &(*temp)->next;
    }
    *temp = new(struct node);
    (*temp)->n = v_j;
    (*temp)->next = NULL;
}
// List all the vertex.
void Graph::listVertex() {
    VPTR temp = V;
    while(temp) {
        cout <<temp->i <<" ";
        temp = temp->next;
    }
    cout <<"\n";

}

// Client program
int main() {
    Graph G;
    G.addEdge(1, 2);
    G.listVertex();

}

С помощью вышеприведенного кода вы можете развернуть, чтобы делать DFS/BFS и т.д.