Ejemplo n.º 1
0
/**
 * Constructor to create a random, connected graph.
 * @param isWeighted - specifies whether the graph is a weighted graph or not
 * @param numVertices - the number of vertices the graph will have
 * @param seed - a random seed to create the graph with
 */
Graph::Graph(bool isWeighted, int numVertices, unsigned long seed)
    : graph(VertexMap()),
      vertexLabels(VertexLabelMap()),
      random(Random(seed)),
      weighted(isWeighted),
      vertexCounter(0)
{
    if (numVertices < 2)
        error("numVertices too low");

    vector<Vertex> vertices;
    for (int i = 0; i < numVertices; ++i) {
        Vertex next = insertVertex();
        vertices.push_back(next);
    }

    // make sure all vertices are connected
    random.shuffle(vertices);
    Vertex cur = vertices[0];
    for (size_t i = 0; i < vertices.size() - 1; ++i) {
        Vertex next = vertices[i + 1];
        insertEdge(cur, next);
        if (weighted) {
            int weight = random.nextInt();
            setEdgeWeight(cur, next, weight);
        }
        cur = next;
    }

    // keep the graph from being overpopulated with edges,
    //  while still maintaining a little randomness
    int numFailures = 0;
    int idx = 0;
    random.shuffle(vertices);
    while (numFailures < 2) {
        if (!insertEdge(vertices[idx], vertices[idx + 1])) {
            ++numFailures;
        } else {
            // if insertEdge() succeeded...
            if (isWeighted)
                setEdgeWeight(vertices[idx], vertices[idx + 1],
                              random.nextInt());
            ++idx;
            if (idx >= numVertices - 2) {
                idx = 0;
                random.shuffle(vertices);
            }
        }
    }
}
Ejemplo n.º 2
0
void MainWindow::keyReleaseEvent(QKeyEvent* e) {
  switch (e->key()) {
    case Qt::Key_0:
      setEdgeWeight(0);
      break;
    case Qt::Key_1:
      setEdgeWeight(1);
      break;
    case Qt::Key_2:
      setEdgeWeight(2);
      break;
    case Qt::Key_3:
      setEdgeWeight(3);
      break;
    case Qt::Key_4:
      setEdgeWeight(4);
      break;
    case Qt::Key_5:
      setEdgeWeight(5);
      break;
    case Qt::Key_6:
      setEdgeWeight(6);
      break;
    case Qt::Key_7:
      setEdgeWeight(7);
      break;
    case Qt::Key_8:
      setEdgeWeight(8);
      break;
    case Qt::Key_9:
      setEdgeWeight(9);
      break;
    case Qt::Key_B:
      graph_->update_bridges();
      reloadModel();
    default:
      QMainWindow::keyReleaseEvent(e);
  }
}