Skip to content

Commit

Permalink
Merge 8a90ede into f392b02
Browse files Browse the repository at this point in the history
  • Loading branch information
MengLiuPurdue committed Dec 14, 2018
2 parents f392b02 + 8a90ede commit 5d4dfb7
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 210 deletions.
19 changes: 12 additions & 7 deletions localgraphclustering/GraphDrawing.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def show(self):
"""
return self.fig

def highlight(self,nodelist,othernodes=False,otheredges=False,filled=False,alpha=0):
def highlight(self,nodelist,othernodes=False,otheredges=False,circled=True,alpha=0.1):
"""
highlight a set of nodes
Expand All @@ -97,19 +97,24 @@ def highlight(self,nodelist,othernodes=False,otheredges=False,filled=False,alpha
otheredges: bool (False by default)
whether to hide edges that doesn't connect two nodes in the nodelist
filled: bool (False by default)
set to True to only circle nodes in the nodelist
circled: bool (False by default)
set to True to circle nodes in the nodelist
alpha: float (1.0 by default)
change alpha for nodes in the nodelist
change alpha for nodes that are not in the nodelist
"""
nodeset = set(nodelist)
if not othernodes or not filled:
if not othernodes or circled:
node_out = list(set(range(self.G._num_vertices)) - nodeset)
if not othernodes:
self.nodecolor(node_out,alpha=alpha)
if not filled:
self.only_circle_nodes(nodelist)
if circled:
self.nodecolor(nodelist,facecolor='r',edgecolor='b',alpha=1)
curr_size = self.nodes_collection.get_sizes()[0]
self.nodesize(nodelist,[(curr_size*1.5)**2]*len(nodelist))
curr_width = self.nodes_collection.get_linewidths()[0]
self.nodewidth(nodelist,[(curr_width*1.5)**2]*len(nodelist))
#self.only_circle_nodes(nodelist)
if not otheredges:
for (i,j) in self.edge_mapping.keys():
if i not in nodeset or j not in nodeset:
Expand Down
2 changes: 1 addition & 1 deletion localgraphclustering/GraphLocal.py
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,7 @@ def draw_groups(self,coords,groups,alpha=1.0,nodesize=5,linewidth=1,
if number_of_colors <= 20:
cm = plt.get_cmap("tab20b")
else:
cm = plt.get_cmap("magma")
cm = plt.get_cmap("gist_ncar")
vmin = 0.0
vmax = 1.0
drawing = GraphDrawing(self,coords,ax=axs,figsize=figsize)
Expand Down
155 changes: 79 additions & 76 deletions localgraphclustering/src/lib/include/maxflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,118 +70,120 @@ bool graph<vtype,itype>::BFS(vtype s, vtype t, vtype V)
// https://www.codeproject.com/Articles/418776/How-to-replace-recursive-functions-using-stack-and

template<typename vtype, typename itype>
double graph<vtype,itype>::sendFlow(vtype init_u, double init_flow, vtype t, vtype start[])
{
struct SnapShot {
//double temp_flow;
vtype u;
double flow;
int stage = 0;
SnapShot(vtype node, double f)
{
u = node;
flow = f;
}
};
double graph<vtype,itype>::sendFlow(vtype init_u, double init_flow, vtype t, vtype start[], pair<int,double>* SnapShots)
{

//pair<int,double> SnapShots[n];

double retVal = 0;

stack<SnapShot> SnapShotStack;
stack<vtype> SnapShotStack;

SnapShot currentSnapShot = SnapShot(init_u,init_flow);
//SnapShot currentSnapShot = SnapShot(init_u,init_flow);
//SnapShot* currentPtr;
SnapShots[init_u].first = 0;
SnapShots[init_u].second = init_flow;

SnapShotStack.push(currentSnapShot);
SnapShotStack.push(init_u);
while (!SnapShotStack.empty()) {
//cout << SnapShotStack.size() << endl;
currentSnapShot=SnapShotStack.top();
SnapShotStack.pop();
vtype u = currentSnapShot.u;
if (start[u] >= adj[u].size()) {
vtype u=SnapShotStack.top();

if (u == t) {
retVal = SnapShots[u].second;
SnapShotStack.pop();
continue;
}

Edge<vtype,itype> &e = adj[u][start[u]];
double flow = currentSnapShot.flow;
switch (currentSnapShot.stage)
double flow = SnapShots[u].second;
switch (SnapShots[u].first)
{
case 0:
//cout << "a" << endl;
//cout << u << " " << start[u] << " " << adj[u].size() << " " << e.v << endl;
currentSnapShot.stage = 1;
SnapShotStack.push(currentSnapShot);
if (u != t && level[e.v] == level[u]+1 && e.flow < e.C && start[e.v] < adj[e.v].size()) {
SnapShots[u].first = 1;
//SnapShotStack.push(u);
if (level[e.v] == level[u]+1 && e.flow < e.C) {
// find minimum flow from u to t
double curr_flow = min(flow, e.C - e.flow);
SnapShot newSnapShot = SnapShot(e.v,curr_flow);
SnapShotStack.push(newSnapShot);
SnapShots[e.v].first = 0;
SnapShots[e.v].second = curr_flow;
//SnapShot newSnapShot = SnapShot(e.v,curr_flow);
SnapShotStack.push(e.v);
}
break;
case 1:
//cout << "b" << endl;
if (u == t) {
retVal = flow;
break;
if (retVal > 0) {
// add flow to current edge
e.flow += retVal;
// subtract flow from reverse edge
// of current edge
adj[e.v][e.rev].flow -= retVal;
}
double temp_flow = (retVal > 0) ? retVal : 0;
//cout << u << " " << start[u] << " " << e.flow << " " << e.v << " " << e.rev << " " << temp_flow << endl;
// add flow to current edge
e.flow += temp_flow;

// subtract flow from reverse edge
// of current edge
adj[e.v][e.rev].flow -= temp_flow;
retVal = temp_flow;
start[u] ++;
if (retVal <= 0 && start[u] < adj[u].size()) {

if (retVal <= 0 && (start[u]+1) < adj[u].size()) {
start[u] ++;
//cout << u << " " << start[u] << " " << adj[u].size() << endl;
Edge<vtype,itype> &new_e = adj[u][start[u]];
SnapShotStack.push(currentSnapShot);
if (u != t && level[new_e.v] == level[u]+1 && new_e.flow < new_e.C && start[new_e.v] < adj[new_e.v].size()) {
//SnapShotStack.push(u);
//SnapShotStack.push(currentPtr);
if (level[new_e.v] == level[u]+1 && new_e.flow < new_e.C) {
double curr_flow = min(flow, new_e.C - new_e.flow);
SnapShot newSnapShot = SnapShot(new_e.v,curr_flow);
SnapShotStack.push(newSnapShot);
SnapShots[new_e.v].first = 0;
SnapShots[new_e.v].second = curr_flow;
SnapShotStack.push(new_e.v);
//SnapShot newSnapShot = SnapShot(new_e.v,curr_flow);
//SnapShotStack.push(&newSnapShot);
}
}
else {
SnapShotStack.pop();
}
break;
}
}

return retVal;
}

/*
template<typename vtype, typename itype>
double graph<vtype,itype>::sendFlow(vtype u, double flow, vtype t, vtype start[])
{
// Sink reached
if (u == t)
return flow;

// template<typename vtype, typename itype>
// double graph<vtype,itype>::sendFlow(vtype u, double flow, vtype t, vtype start[])
// {
// //cout << u << " " << start[u] << endl;
// // Sink reached
// if (u == t)
// return flow;

// Traverse all adjacent edges one -by - one.
for ( ; start[u] < adj[u].size(); start[u]++) {
// Pick next edge from adjacency list of u
Edge<vtype,itype> &e = adj[u][start[u]];
// // Traverse all adjacent edges one -by - one.
// for ( ; start[u] < adj[u].size(); start[u]++) {
// // Pick next edge from adjacency list of u
// Edge<vtype,itype> &e = adj[u][start[u]];

if (level[e.v] == level[u]+1 && e.flow < e.C) {
// find minimum flow from u to t
double curr_flow = min(flow, e.C - e.flow);
// if (level[e.v] == level[u]+1 && e.flow < e.C) {
// // find minimum flow from u to t
// double curr_flow = min(flow, e.C - e.flow);

double temp_flow = sendFlow(e.v, curr_flow, t, start);
// double temp_flow = sendFlow(e.v, curr_flow, t, start);

// flow is greater than zero
if (temp_flow > 0) {
// add flow to current edge
e.flow += temp_flow;
// // flow is greater than zero
// if (temp_flow > 0) {
// // add flow to current edge
// e.flow += temp_flow;

// subtract flow from reverse edge
// of current edge
adj[e.v][e.rev].flow -= temp_flow;
return temp_flow;
}
}
}
// // subtract flow from reverse edge
// // of current edge
// adj[e.v][e.rev].flow -= temp_flow;
// //cout << u << " " << temp_flow << endl;
// return temp_flow;
// }
// }
// }

return 0;
}
*/
// return 0;
// }


template<typename vtype, typename itype>
Expand Down Expand Up @@ -241,6 +243,7 @@ pair<double,vtype> graph<vtype,itype>::DinicMaxflow(vtype s, vtype t, vtype V, v
// from source to sink
vtype *start = NULL;
//cout << INT_MAX << endl;
pair<int,double> SnapShots[n];
while (BFS(s, t, V) == true){
// store how many edges are visited
// from V { 0 to V }
Expand All @@ -251,12 +254,12 @@ pair<double,vtype> graph<vtype,itype>::DinicMaxflow(vtype s, vtype t, vtype V, v
fill(start,start+V+1,0);
//cout << "here" << endl;
// while flow is not zero in graph from S to D
double flow = sendFlow(s, INT_MAX, t, start);
double flow = sendFlow(s, INT_MAX, t, start, SnapShots);
//cout << flow << endl;
while (flow > 0) {
// Add path flow to overall flow
total += flow;
flow = sendFlow(s, INT_MAX, t, start);
flow = sendFlow(s, INT_MAX, t, start, SnapShots);
}
//cout << "BFS" << endl;
}
Expand Down
2 changes: 1 addition & 1 deletion localgraphclustering/src/lib/include/routines.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class graph{
pair<double, double> get_stats_weighted(unordered_map<vtype, vtype>& R_map, vtype nR);
void addEdge(vtype u, vtype v, double C);
bool BFS(vtype s, vtype t, vtype V);
double sendFlow(vtype u, double flow, vtype t, vtype start[]);
double sendFlow(vtype u, double flow, vtype t, vtype start[], pair<int,double>* SnapShots);
pair<double,vtype> DinicMaxflow(vtype s, vtype t, vtype V, vector<bool>& mincut);
void find_cut(vtype u, vector<bool>& mincut, vtype& length);

Expand Down
Loading

0 comments on commit 5d4dfb7

Please sign in to comment.