【问题标题】:How to randomly pick a vertices from graph?如何从图中随机选择一个顶点?
【发布时间】:2016-07-28 23:52:23
【问题描述】:

我正在寻找使用 c++ 从图形中随机选择一个数字的解决方案。
例如,我有一个在两个顶点之间添加边(一个或多个)的图,我如何随机选择一个数字?
一些代码:

#include <iostream>
#include <list>
#include <queue>

using namespace std;
// Graph class represents a undirected graph using adjacency list representation
class Graph
{
private:
    int V; // # of vertices
    list<int> *adj;  // Pointer to an array containing adjacency lists
public:
    Graph(int V)  // Constructor
    {
        this->V = V;
        adj = new list<int>[V];
    }
    void addEdge(int v, int w); // function to add an edge to graph
    void print(int v, int w); //function to display 
    };

void Graph::addEdge(int v, int w)
{
    adj[v].push_front(w); // Add w to v’s list.
    adj[w].push_front(v); // Add v to w’s list.
    print(v, w);

}

void Graph::print(int v, int w) {
cout << v << " - " << w << endl;}


主要是:

Graph g(4);
    g.addEdge(0, 1);
    g.addEdge(0, 2);
    g.addEdge(1, 3);


示例输出:

0 - 1
0 - 2
1 - 3

【问题讨论】:

  • 离题了,但你为什么要为adj 使用裸c 样式数组?为什么不是std::vectorstd::array?这样您就不需要使用V 手动跟踪大小或手动删除它。
  • @MarkH 我正在使用“向量”添加顶点,所以如果有一些提示如何?
  • 您可以使用vector&lt; list&lt;int&gt; &gt;,而不是list&lt;int&gt; *adj。然后,在构造函数中,您将使用adj = vector&lt; list&lt;int&gt; &gt;(V) 来设置向量的大小。最好使用初始化列表:Graph(int V) : adj(V) {}。参考:en.cppreference.com/w/cpp/language/initializer_list
  • @MarkH 如何改变 adj[v].push_front(w); ?
  • 那行保持不变。

标签: c++ random graph


【解决方案1】:

使用数学库。兰德函数。选择随机数并从边列表中选择(0到边数-1),然后选择另一个随机数并选择边的顶点,0或1(边的底部/顶部顶点)

【讨论】:

  • 谢谢!请给我更多提示好吗!
  • 添加访问边和边顶点的函数。然后这样做 g.getRandEdge().getRandVertex()
  • 您在列表中以对形式存储边,所以只需这样做(列表中的顶点总数 / 2 ,取一个介于 0 和 0 之间的随机数,然后乘以 2,然后添加一个0或1范围内的随机数获取任意一个顶点
【解决方案2】:
#include <stdlib.h> 
#include <iostream>
#include <list>
#include <queue>

using namespace std;
// Graph class represents a undirected graph using adjacency list representation
class Graph
{
private:
    int V; // # of vertices
    list<int> *adj;  // Pointer to an array containing adjacency lists
public:
    Graph(int V)  // Constructor
    {
        this->V = V;
        adj = new list<int>[V];
    }
    void addEdge(int v, int w); // function to add an edge to graph
    void print(int v, int w); //function to display 
    };

void Graph::addEdge(int v, int w)
{
    adj[v].push_front(w); // Add w to v’s list.
    adj[w].push_front(v); // Add v to w’s list.
    print(v, w);

}

int Graph::getRandomVertexFromEdge()
{
    int list_size_divided_by_2 = adj.size() / 2;
    int rEdge = (rand() % list_size_divided_by_2);
    int rVW = (rand() % 1);
    int ret = adj[(rEdge + rVW)];
    //this will return a random vertex from a random edge;
    print(rEdge,rVW);
    return ret;
}

void Graph::print(int v, int w) {
cout << v << " - " << w << endl;}

【讨论】:

  • @Bsh 请查看这个未经测试的实现。
  • 我现在正在测试,但这里有些错误(1- int list_size_divided_by_2 = adj.size() / 2; 我更正为 adj[V].size()....)和( 2- int ret = adj[(rEdge + rVW)];) 不能从列表类型转换为 int !
  • 正如我所说,它未经测试并按原样提供,但仅供您了解如何操作。
  • 同样,我在 rand 部分之后将 rEdge var 乘以 2,
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-23
  • 2020-10-04
  • 2019-12-03
相关资源
最近更新 更多