aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFranklin Wei <me@fwei.tk>2018-11-12 12:18:25 -0500
committerFranklin Wei <me@fwei.tk>2018-11-12 12:18:25 -0500
commitea945ff4ffae82bb1ee77ec28ce373ba4525d283 (patch)
tree4566e1840d277cbfb93ae0dc1f3d87c68e51f28e
parentd0c2beb062bb267d35c5d2c800ae448f33b65868 (diff)
downloadcircgraph-ea945ff4ffae82bb1ee77ec28ce373ba4525d283.zip
circgraph-ea945ff4ffae82bb1ee77ec28ce373ba4525d283.tar.gz
circgraph-ea945ff4ffae82bb1ee77ec28ce373ba4525d283.tar.bz2
circgraph-ea945ff4ffae82bb1ee77ec28ce373ba4525d283.tar.xz
Change graph description format
Also adds a `genrand' program for testing purposes.
-rw-r--r--README.md7
-rw-r--r--genrand.c19
-rw-r--r--main.cpp61
-rw-r--r--test1.txt8
-rw-r--r--test2.txt11
-rw-r--r--test3.txt11
-rw-r--r--test4.txt14
-rw-r--r--test5.txt8
-rw-r--r--test6.txt51
9 files changed, 148 insertions, 42 deletions
diff --git a/README.md b/README.md
index 3f11ac1..e8c602d 100644
--- a/README.md
+++ b/README.md
@@ -23,10 +23,9 @@ g++ main.cpp -o circgraph
```
<source_node> <sink_node>
-<node1> [<neighbor1> <resistance2>]...
-<node2>
+<node1> <node 2> <weight>
+<node1> <node 2> <weight>
...
-<noden>
```
-See the `testX.txt` files for more.
+See the `testX.txt` files for examples.
diff --git a/genrand.c b/genrand.c
new file mode 100644
index 0000000..47362eb
--- /dev/null
+++ b/genrand.c
@@ -0,0 +1,19 @@
+#include <stdio.h>
+#include <math.h>
+
+int main(int argc, char *argv[])
+{
+ if(argc != 2)
+ {
+ fprintf(stderr, "Usage: %s EDGES\n", argv[0]);
+ return 1;
+ }
+
+ int edges = atoi(argv[1]);
+
+ srand(time(0));
+
+ printf("a b\n");
+ for(int i = 0; i < edges; i++)
+ printf("%c %c 1\n", 'a' + rand() % 26, 'a' + rand() % 26);
+}
diff --git a/main.cpp b/main.cpp
index 1f27558..0064a92 100644
--- a/main.cpp
+++ b/main.cpp
@@ -28,6 +28,7 @@ struct edge {
};
struct node {
+ string name;
vector<edge> neighbors;
};
@@ -137,6 +138,15 @@ node &insert_node(map<int, node> &graph, int id)
return it->second;
}
+node &insert_node_if_new(map<int, node> &graph, int id)
+{
+ map<int, node>::iterator it = graph.find(id);
+ if(it == graph.end())
+ return insert_node(graph, id);
+ else
+ return it->second;
+}
+
void erase_edges(map<int, node> &graph, int id_a, int id_b)
{
cerr << "Erasing edges " << id_a << "-" << id_b << endl;
@@ -398,34 +408,57 @@ double simp_graph(map<int,node> &graph, int s, int t)
return graph[s].neighbors[0].weight;
}
+int get_node_id(map<string, int> &names, string name, int &counter)
+{
+ const map<string, int>::const_iterator it = names.find(name);
+ if(it == names.end())
+ return names[name] = counter++;
+ else
+ return it->second;
+}
+
int main()
{
/* construct graph from adjacency list */
map<int, node> graph;
string line;
- int s, t;
- cin >> s >> t;
+ map<string, int> names;
+ string s, t;
+
+ getline(cin, line);
+ stringstream ss(line);
+ ss >> s >> t;
+
+ cerr << "Source and sink are " << s << ", " << t << endl;
+
+ int id_counter = 1;
while(getline(cin, line))
{
- stringstream ss(line);
+ ss = stringstream(line);
- int node_id;
- struct node n;
- ss >> node_id;
+ double weight;
+ string a, b;
+ ss >> a >> b >> weight;
- struct edge e;
- while(ss >> e.id && ss >> e.weight)
- {
- cerr << "Adding neighbor " << e.id << " with weight " << e.weight << endl;
+ int id_a, id_b;
+ id_a = get_node_id(names, a, id_counter);
+ id_b = get_node_id(names, b, id_counter);
- n.neighbors.push_back(e);
- }
+ insert_node_if_new(graph, id_a);
+ insert_node_if_new(graph, id_b);
+
+ graph[id_a].name = a;
+ graph[id_b].name = b;
- graph.insert(std::pair<int, node>(node_id, n));
+ insert_edge(graph, id_a, id_b, weight);
}
- double eq = simp_graph(graph, s, t);
+ int id_s, id_t;
+ id_s = names.at(s);
+ id_t = names.at(t);
+
+ double eq = simp_graph(graph, id_s, id_t);
cerr << "Equivalent resistance: " << eq << endl;
}
diff --git a/test1.txt b/test1.txt
index 0dea2d9..c9fdb03 100644
--- a/test1.txt
+++ b/test1.txt
@@ -1,6 +1,6 @@
1 5
1 2 1
-2 1 1 3 1 4 1
-3 2 1 4 1
-4 2 1 3 1 5 1
-5 4 1
+2 3 1
+2 4 1
+3 4 1
+4 5 1
diff --git a/test2.txt b/test2.txt
index 4d4506e..2bc0bf7 100644
--- a/test2.txt
+++ b/test2.txt
@@ -1,5 +1,6 @@
-1 4
-1 2 1 3 1
-2 1 1 3 1 4 1
-3 1 1 2 1 4 1
-4 3 1 2 1
+a d
+a b 1
+a c 1
+b c 1
+b d 1
+c d 1
diff --git a/test3.txt b/test3.txt
index 007cb00..e6ba9b6 100644
--- a/test3.txt
+++ b/test3.txt
@@ -1,5 +1,6 @@
-1 4
-1 2 4 3 3.125
-2 1 4 3 3 4 4
-3 1 3.125 2 3 4 5
-4 3 5 2 4
+a d
+a b 4
+a c 3.125
+b c 3
+b d 4
+c d 5
diff --git a/test4.txt b/test4.txt
index 3f583d4..3b57188 100644
--- a/test4.txt
+++ b/test4.txt
@@ -1,6 +1,8 @@
-1 5
-1 2 1 5 10
-2 1 1 4 2 3 3
-3 2 3 4 5 5 1.5
-4 2 2 3 5 5 5
-5 1 10 4 5 3 1.5
+a e
+a b 1
+a e 10
+b c 3
+b d 2
+c d 5
+d e 5
+c e 1.5
diff --git a/test5.txt b/test5.txt
index 3e5d639..1f11f98 100644
--- a/test5.txt
+++ b/test5.txt
@@ -1,4 +1,4 @@
-1 3
-1 2 1 3 1
-2 1 1 3 1
-3 1 1 2 1
+a c
+a b 1
+a c 1
+b c 1
diff --git a/test6.txt b/test6.txt
new file mode 100644
index 0000000..28c772b
--- /dev/null
+++ b/test6.txt
@@ -0,0 +1,51 @@
+a b
+t h 1
+t t 1
+s w 1
+g y 1
+w e 1
+i t 1
+n e 1
+o j 1
+q v 1
+i k 1
+q g 1
+z o 1
+t r 1
+w k 1
+c s 1
+a e 1
+a v 1
+u v 1
+v u 1
+y a 1
+u t 1
+x i 1
+t j 1
+e n 1
+a l 1
+s n 1
+d q 1
+j t 1
+f y 1
+q i 1
+o i 1
+f s 1
+n o 1
+k z 1
+c k 1
+d k 1
+t y 1
+h b 1
+q o 1
+a n 1
+a s 1
+j u 1
+p f 1
+d u 1
+c v 1
+d u 1
+m r 1
+h i 1
+k c 1
+m s 1