Graphlite GitHub Address Https://github.com/schencoding/GraphLite
Diagram is very suitable for distributed parallel computing, such as the shortest path, PageRank and other problems, the more famous graph calculation framework has PREGE,CMU Graphlab,apache giraph and so on. Graphlite belongs to the BSP model.
The BSP model is shown below:
Also known as the hyper-step calculation, Apache Giraph belongs to this mode.
Figure coloring in a single-machine environment of the algorithm, the fastest is generally greedy algorithm, that is, every time to find the nodes are not adjacent to coloring, until the completion of all. In the Distributed parallel computing environment, we also use greedy algorithm, every time to find all the nodes are not adjacent to the same color, in 4 hyper-step to complete the coloring, the first step according to the size of the scale to choose which nodes may be shaded, the second process of conflict, the third step to delete the selected node and the neighbor node between the edge, The fourth step is to dye the selected nodes.
#include <stdio.h> #include <string.h> #include <math.h> #include <list> #include "GraphLite.h" #define VERTEX_CLASS_NAME (NAME) graphcolorvertex# #name #define NUM_VERTICES_COLORED 0 #define Num_vertices_unk Nown 1 #define NUM_VERTICES_IN_SET 2 #define NUM_VERTICES_NOT_IN_SET 3 #define Num_verti
Ces_tentatively_in_set 4 enum Phase {lottery, conflict_resolution, edge_cleaning, color_assignment};
Enum State {UNKNOWN, Tentatively_in_set, Not_in_set, in_set};
Enum MessageType {wants_to_be_in_set, is_in_set};
struct Colorvertex {long long ID;
int color;
int iscolored;
Enum state State;
int isDeleted;
int edgenums;
Phase Phase;
List<long long> Target;
};
struct Message {long long senderId;
Enum MessageType message;
};
Long Long V0;
Phase Phase;
int colorid = 1; Class Vertex_class_name (Inputformatter): public Inputformatter {public:int64_t getvertexnum () {unsigned lon G Long N;
SSCANF (M_ptotal_vertex_line, "%lld", &n);
m_total_vertex= N;
return M_total_vertex;
} int64_t Getedgenum () {unsigned long long n;
SSCANF (M_ptotal_edge_line, "%lld", &n);
m_total_edge= N;
return M_total_edge;
} int getvertexvaluesize () {m_n_value_size = sizeof (Colorvertex);
return m_n_value_size;
} int getedgevaluesize () {m_e_value_size = sizeof (int);
return m_e_value_size;
} int getmessagevaluesize () {m_m_value_size = sizeof (struct Message);
return m_m_value_size;
} void Loadgraph () {unsigned long long last_vertex;
unsigned long long from;
unsigned long long to;
int weight = 0;
Colorvertex Vertex;
int outdegree = 0;
const char *line= getedgeline ();
Note:modify this if a edge weight is to be read//Modify the ' weight ' variable SSCANF (line, "%lld%lld", &from, &to);
Addedge (from, to, &weight);
Vertex.color =-1;
vertex.isdeleted = 0;
Last_vertex = from;
++outdegree;
for (int64_t i = 1; i < M_total_edge; ++i) {line= getedgeline (); Note:modify this if a edge weight is to be read//Modify the ' weight ' variable sscanf
(Line, "%lld%lld", &from, &to);
if (Last_vertex! = from) {Addvertex (Last_vertex, &vertex, Outdegree);
Last_vertex = from;
Outdegree = 1;
} else {++outdegree;
} Addedge (from, to, &weight);
} Addvertex (Last_vertex, &vertex, Outdegree);
}
};
Class Vertex_class_name (Outputformatter): public Outputformatter {public:void Writeresult () {int64_t vid;
Colorvertex value;
Char s[1024]; for (Resultiterator R_iter; ! R_iter.done ();
R_iter.next ()) {R_iter.getidvalue (vid, &value);
int n = sprintf (S, "%LLD:%d\n", (unsigned long long) vid, value.color);
Writenextresline (s, N);
}
}
}; An aggregator, records a double value Tom Compute sum class Vertex_class_name (aggregator): Public Aggregator<lon
G long> {public:void init () {m_global = 0;
m_local = 0;
} void* Getglobal () {return &m_global;
} void Setglobal (const void* p) {m_global = * (Long long *) p;
} void* getlocal () {return &m_local;
} void Merge (const void* p) {M_global + = * (Long long *) p;
} void Accumulate (const void* p) {m_local + = * (Long long *) p;
}
}; Class Vertex_class_name (): Public VERTEX <colorvertex, int, message> {public:void compute (messageiterator* PMS
GS) {Long Long acc = 1;
Colorvertex tmp = GetValue ();
Message msg; Long Long myId = Getvertexid ();
Long Long MiniD = myId;
int numneighborsmovedintoset = 0;
Double random;
double probability;
List<long Long>::iterator it;
Outedgeiterator iter = Getoutedgeiterator ();
printf ("fcefef\n");
if (getsuperstep () = = 0) {tmp.id = Getvertexid ();
Tmp.phase = lottery;
if (tmp.id = = V0) {tmp.color = 0;
tmp.iscolored = 1;
Tmp.state = In_set;
Tmp.edgenums = Getoutedgeiterator (). Size ();
} else {tmp.color =-1;
tmp.iscolored = 0;
Tmp.state = UNKNOWN;
Tmp.edgenums = Getoutedgeiterator (). Size ();
}} else {if (GetValue (). iscolored) {Votetohalt ();
Return
state state = GetValue (). State;
Switch (tmp.phase) {case lottery:printf ("lottery\n");
Switch (state) {case Unknown:srand (unsigned) time (NULL)); Random = (double) Rand()/rand_max/10000;
if (tmp.edgenums = = 0) {tmp.state = In_set;
} else if (random * tmp.edgenums <= 1.0) {tmp.state = Tentatively_in_set;
Msg.senderid = Getvertexid ();
Msg.message = Wants_to_be_in_set;
Sendmessagetoallneighbors (msg);
} break;
Default:break;
} tmp.phase = Conflict_resolution;
Break
Case Conflict_resolution:switch (state) {case Tentatively_in_set: if (!pmsgs->done ()) {for (;! pmsgs->done (); Pmsgs->next ()) {if (pmsgs->
; GetValue (). Message = = Wants_to_be_in_set) {Long Long Neighborid = Pmsgs->getvalue (). SenderId;
if (Neighborid < MiniD) {MiniD = Neighborid;
}
} } if (MiniD = = myId) {tmp.state = In_set;
tmp.isdeleted = 1;
Msg.senderid = Getvertexid ();
Msg.message = Is_in_set;
Sendmessagetoallneighbors (msg);
} else {tmp.state = UNKNOWN;
}} else {tmp.state = In_set;
tmp.isdeleted = 1;
Msg.senderid = Getvertexid ();
Msg.message = Is_in_set;
Sendmessagetoallneighbors (msg);
} break;
Default:break;
} tmp.phase = edge_cleaning;
Break
Case Edge_cleaning:for (;! pmsgs->done (); Pmsgs->next ()) {if (Pmsgs->getvalue (). Message = = Is_in_set) {
Tmp.target.remove (Pmsgs->getvalue (). senderId);
++numneighborsmovedintoset;
}} if (Numneighborsmovedintoset > 0) {tmp.edgenums-= Numneighborsmovedintoset;
Tmp.state = Not_in_set;
} Tmp.phase = color_assignment;
Break
Case Color_assignment:if (state = = In_set) {Tmp.color = Getsuperstep ()/4;
tmp.iscolored = 1;
Accumulateaggr (0, &ACC);
} else {tmp.state = UNKNOWN;
} tmp.phase = lottery;
Break
Default:break; } switch (GetValue (). State) {case UNKNOWN:ACCUMULATEAGGR (Num_vertices_unknown,
&ACC);
Break
Case Tentatively_in_set:accumulateaggr (Num_vertices_tentatively_in_set, &ACC);
Break
Case Not_in_set:accumulateaggr (Num_vertices_not_in_set, &ACC);
Break
Case In_set:accumulateaggr (Num_vertices_in_set, &ACC);
Break
Default:break;
}} *mutablevalue () = tmp;
}
}; Class Vertex_class_name (graph): public Graph {
Public:vertex_class_name (aggregator) * aggregator; Public://argv[0]: pagerankvertex.so//argv[1]: <input path>//argv[2]: <output path> void
init (int argc, char* argv[]) {setnumhosts (5);
Sethost (0, "localhost", 1411);
Sethost (1, "localhost", 1421);
Sethost (2, "localhost", 1431);
Sethost (3, "localhost", 1441);
Sethost (4, "localhost", 1451);
if (ARGC < 3) {printf ("Usage:%s <input path> <output path>\n", argv[0]);
Exit (1);
} M_pin_path = argv[1];
M_pout_path = argv[2];
Phase = Lottery;
aggregator = new Vertex_class_name (aggregator) [5];
REGNUMAGGR (5);
for (int i = 0; i < 5; i++) Regaggr (i, &aggregator[i]);
} void Term () {delete[] aggregator;
}
}; /* Stop:do not change the code below.
*/extern "C" graph* create_graph () {graph* pgraph = new Vertex_class_name (Graph); Pgraph->m_pin_formatter = new Vertex_class_name (inputformatter);
Pgraph->m_pout_formatter = new Vertex_class_name (outputformatter);
Pgraph->m_pver_base = new Vertex_class_name ();
return pgraph;
} extern "C" void Destroy_graph (graph* pobject) {Delete (Vertex_class_name () *) (pobject->m_pver_base);
Delete (Vertex_class_name (outputformatter) *) (Pobject->m_pout_formatter);
Delete (Vertex_class_name (inputformatter) *) (Pobject->m_pin_formatter);
Delete (Vertex_class_name (Graph) *) Pobject;
}