Skip to content

Commit 4dcf7e6

Browse files
committed
add transpose of a directed graph
1 parent 95faee5 commit 4dcf7e6

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

include/directed_graph.h

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,13 @@
2626
#include "graph_defs.h"
2727
#include "double_linked_list.h"
2828

29-
namespace alg
30-
{
29+
namespace alg {
3130
class DirectedGraph:public Graph {
3231
private:
3332
/**
3433
* delete a vertex from adjacent lists
3534
*/
36-
void delete_me(uint32_t id)
37-
{
35+
void delete_me(uint32_t id) {
3836
// delete every connection, iterator through every adjacent list
3937
Adjacent * adj;
4038
list_for_each_entry(adj, &a_head, a_node){
@@ -77,8 +75,7 @@ namespace alg
7775
/**
7876
* delete a vertex with specified id
7977
*/
80-
void delete_vertex(uint32_t id)
81-
{
78+
void delete_vertex(uint32_t id) {
8279
Adjacent * a = (*this)[id];
8380
if (a==NULL) return;
8481
delete_me(id);
@@ -92,8 +89,7 @@ namespace alg
9289
/**
9390
* create a new vertex and add to the graph, with specified id.
9491
*/
95-
bool add_vertex(uint32_t id)
96-
{
92+
bool add_vertex(uint32_t id) {
9793
if ((*this)[id]!=NULL) return false;
9894

9995
// new empty adjacent list
@@ -107,8 +103,7 @@ namespace alg
107103
/**
108104
* add an edge for x -> y
109105
*/
110-
bool add_edge(uint32_t x, uint32_t y, int32_t weight)
111-
{
106+
bool add_edge(uint32_t x, uint32_t y, int32_t weight) {
112107
struct Adjacent * a1 = (*this)[x];
113108
struct Adjacent * a2 = (*this)[y];
114109

@@ -130,8 +125,7 @@ namespace alg
130125
/**
131126
* delete an edge for x -> y
132127
*/
133-
void delete_edge(uint32_t x, uint32_t y)
134-
{
128+
void delete_edge(uint32_t x, uint32_t y) {
135129
struct Adjacent * a1 = (*this)[x];
136130
struct Adjacent * a2 = (*this)[y];
137131
if (a1==NULL || a2==NULL) return ;
@@ -148,6 +142,24 @@ namespace alg
148142
}
149143
}
150144
}
145+
146+
/**
147+
* create the transpose of a directed-graph
148+
*/
149+
DirectedGraph * tranpose() {
150+
DirectedGraph * trans = new DirectedGraph;
151+
Adjacent * a;
152+
list_for_each_entry(a, &a_head, a_node){
153+
trans->add_vertex(a->v.id);
154+
Vertex * v;
155+
list_for_each_entry(v, &a->v_head, v_node){
156+
trans->add_vertex(v->id);
157+
trans->add_edge(v->id, a->v.id, v->weight);
158+
}
159+
}
160+
161+
return trans;
162+
}
151163
};
152164
}
153165

0 commit comments

Comments
 (0)