[Lemon-commits] [lemon_svn] marci: r731 - in hugo/trunk/src/work: jacint marci
Lemon SVN
svn at lemon.cs.elte.hu
Mon Nov 6 20:41:12 CET 2006
Author: marci
Date: Thu May 6 19:22:11 2004
New Revision: 731
Modified:
hugo/trunk/src/work/jacint/graph_gen.h
hugo/trunk/src/work/marci/bipartite_graph_wrapper.h
hugo/trunk/src/work/marci/bipartite_matching_try_3.cc
Log:
random graph, random bipartite graph in jacint/graph_gen.h
Modified: hugo/trunk/src/work/jacint/graph_gen.h
==============================================================================
--- hugo/trunk/src/work/jacint/graph_gen.h (original)
+++ hugo/trunk/src/work/jacint/graph_gen.h Thu May 6 19:22:11 2004
@@ -1,16 +1,7 @@
// -*- c++ -*-
-//randomGraph(i,j) gives a random graph on i nodes and j edges.
#include <vector>
#include <cstdlib>
-//#include <list_graph.h>
-//#include <time_measure.h>
-//#include <for_each_macros.h>
-//#include <bfs_iterator.h>
-//#include <bipartite_graph_wrapper.h>
-//#include <maps.h>
-//#include <max_flow.h>
-
namespace hugo {
@@ -38,15 +29,36 @@
}
+ /// Generates a random graph with n nodes and m edges.
+ /// Before generating the random graph, \c g.clear() is called.
template<typename Graph>
- void randomGraph (Graph& g, int n, int m) {
- typedef typename Graph::Node Node;
+ void randomGraph(Graph& g, int n, int m) {
g.clear();
- std::vector<Node> nodes;
+ std::vector<typename Graph::Node> nodes;
for (int i=0; i<n; ++i)
nodes.push_back(g.addNode());
for (int i=0; i<m; ++i)
g.addEdge(nodes[random(n)], nodes[random(n)]);
}
-}
+ /// Generates a random bipartite graph with a and b nodes
+ /// in the color classes and m edges.
+ /// According to the bipartite graph concept, the resulting
+ /// graph is directed from the first class to the second one.
+ /// Before generating the random graph, \c g.clear() is called.
+ template<typename Graph>
+ void randomBipartiteGraph(Graph& g, int a, int b, int m) {
+ g.clear();
+ std::vector<typename Graph::Node> s_nodes;
+ std::vector<typename Graph::Node> t_nodes;
+ for (int i=0; i<a; ++i)
+ ///\bug g.addNode(g.S_CLASS) would be better.
+ s_nodes.push_back(g.addNode(false));
+ for (int i=0; i<b; ++i)
+ ///\bug g.addNode(g.T_CLASS) would be better.
+ t_nodes.push_back(g.addNode(true));
+ for (int i=0; i<m; ++i)
+ g.addEdge(s_nodes[random(a)], t_nodes[random(b)]);
+ }
+
+} //namespace hugo
Modified: hugo/trunk/src/work/marci/bipartite_graph_wrapper.h
==============================================================================
--- hugo/trunk/src/work/marci/bipartite_graph_wrapper.h (original)
+++ hugo/trunk/src/work/marci/bipartite_graph_wrapper.h Thu May 6 19:22:11 2004
@@ -247,8 +247,8 @@
}
void clear() {
- FOR_EACH_LOC(typename Parent::EdgeIt, e, G) erase(e);
- FOR_EACH_LOC(typename Parent::NodeIt, n, G) erase(n);
+ FOR_EACH_LOC(typename Parent::EdgeIt, e, *this) erase(e);
+ FOR_EACH_LOC(typename Parent::NodeIt, n, *this) erase(n);
}
};
Modified: hugo/trunk/src/work/marci/bipartite_matching_try_3.cc
==============================================================================
--- hugo/trunk/src/work/marci/bipartite_matching_try_3.cc (original)
+++ hugo/trunk/src/work/marci/bipartite_matching_try_3.cc Thu May 6 19:22:11 2004
@@ -2,7 +2,6 @@
#include <iostream>
#include <fstream>
#include <vector>
-#include <cstdlib>
#include <list_graph.h>
//#include <smart_graph.h>
@@ -13,6 +12,7 @@
#include <bipartite_graph_wrapper.h>
#include <hugo/maps.h>
#include <max_flow.h>
+#include <graph_gen.h>
using namespace hugo;
@@ -79,27 +79,6 @@
int matchingValue() { return mf.flowValue(); }
};
-/**
- * Inicializalja a veletlenszamgeneratort.
- * Figyelem, ez nem jo igazi random szamokhoz,
- * erre ne bizzad a titkaidat!
- */
-void random_init()
-{
- unsigned int seed = getpid();
- seed |= seed << 15;
- seed ^= time(0);
-
- srand(seed);
-}
-
-/**
- * Egy veletlen int-et ad vissza 0 es m-1 kozott.
- */
-int random(int m)
-{
- return int( double(m) * rand() / (RAND_MAX + 1.0) );
-}
int main() {
//typedef UndirListGraph Graph;
@@ -113,9 +92,6 @@
Graph g;
- std::vector<Graph::Node> s_nodes;
- std::vector<Graph::Node> t_nodes;
-
int a;
std::cout << "number of nodes in the first color class=";
std::cin >> a;
@@ -127,13 +103,8 @@
std::cin >> m;
std::cout << "Generatig a random bipartite graph..." << std::endl;
- for (int i=0; i<a; ++i) s_nodes.push_back(g.addNode(false));
- for (int i=0; i<b; ++i) t_nodes.push_back(g.addNode(true));
-
random_init();
- for(int i=0; i<m; ++i) {
- g.addEdge(s_nodes[random(a)], t_nodes[random(b)]);
- }
+ randomBipartiteGraph(g, a, b, m);
// std::cout << "Edges of the bipartite graph:" << std::endl;
// FOR_EACH_LOC(EdgeIt, e, g) std::cout << e << " ";
More information about the Lemon-commits
mailing list