diff --git a/22-bipartite/mcbm.pdf b/22-bipartite/mcbm.pdf index 2ee5c8d..6496b4c 100644 Binary files a/22-bipartite/mcbm.pdf and b/22-bipartite/mcbm.pdf differ diff --git a/22-bipartite/mcbm.tex b/22-bipartite/mcbm.tex index 5247f3f..1e191fc 100644 --- a/22-bipartite/mcbm.tex +++ b/22-bipartite/mcbm.tex @@ -34,7 +34,7 @@ \tikzset{snake it/.style={decorate, decoration=snake}} \tikzset{edgelabel/.style = {midway,rectangle, inner sep=0pt, draw=none, fill=white}} -\lstset{language=Java, basicstyle=\footnotesize} +\lstset{language=C++, basicstyle=\footnotesize} \lstset{ % backgroundcolor=\color{white}, % choose the background color; you must add \usepackage{color} or \usepackage{xcolor} basicstyle=\tiny, % the size of the fonts that are used for the code @@ -460,26 +460,32 @@ \begin{frame}[fragile]{Algorithm} \begin{lstlisting} -boolean isBipartite(int n, LinkedList[] successors) { - //successors[i] contains the successors id of node i - int[] color = new int[n]; - for(int i = 0; i < n; i++) color[i] = -1; //init to -1 - - for(int i = 0; i < n; i++) - if(color[i] == -1) //not visited yet - if(!visit(i, successors, color, 1)) - return false; - return true; +bool visit(int, const vector>&, vector&, int); // defined below + +bool isBipartite(int n, const vector>& successors) { + //successors[i] contains the successors of node i + vector color(n, -1); // initialized to -1 + + for (int i = 0; i < n; i++) { + if (color[i] == -1) + if (!visit(i, successors, color, 1)) + return false; + } + return true; } -boolean visit(int node, LinkedList[] successors, int[] color, int parentColor) { - if(color[node] == parentColor) //fail! - return false; - color[node] = (parentColor+1)%2; - for(int next: successors[node]) - if(!visit(next, successors, color, color[node])) - return false; - return true; +bool visit(int node, const vector>& successors, vector& color, int parentColor) { + if (color[node] == parentColor) // failure + return false; + + if (color[node] != -1) // avoid infinite looping + return true; + + color[node] = (parentColor + 1) % 2; + for (int next : successors[node]) + if (!visit(next, successors, node, color[node])) + return false; + return true; } \end{lstlisting} \end{frame} @@ -826,23 +832,22 @@ // succ[i] contains the nodes that can be reached from i // initially, all the edges are from U->V // inU[i] is true iff i is in U -boolean findAndReverse(int n, List[] succ, boolean[] inU) { - int[] pred = new int[n]; - boolean[] visited = new boolean[n]; - Stack todo = new Stack<>(); +bool findAndReverse(int n, vector>& succ, vector& inU) { + vector pred(n); + vector visited(n, false); + stack todo; // Find free nodes in U - boolean[] isFree = new boolean[n]; - Arrays.fill(isFree, true); - for(int i = 0; i < n; i++) - if(!inU[i]) - for(int s: succ[i]) - isFree[s] = false; - - for(int i = 0; i < n; i++) { - if(inU[i] && isFree[i]) { - todo.add(i); - pred[i] = -1. + vector isFree(n, true); + for (int i = 0; i < n; i++) + if (!inU[i]) + for (int s : succ[i]) + isFree[s] = false; + + for (int i = 0; i < n; i++) { + if (inU[i] && isFree[i]) { + todo.push(i); + pred[i] = -1; } }\end{lstlisting} \end{frame} @@ -851,8 +856,8 @@ \begin{lstlisting}[firstnumber=23] // Run the DFS int found = -1; - while(!todo.isEmpty()) { - int node = todo.pop(); + while(!todo.empty()) { + int node = todo.top(); todo.pop(); if(visited[node]) continue; visited[node] = true; @@ -865,7 +870,7 @@ for(int next: successors[node]) { if(!visited[next]) { pred[next] = node; - todo.add(next); + todo.push(next); } } } @@ -874,8 +879,8 @@ // Reverse the nodes if(found != -1) { while(predecessors[found] != -1) { - succ[pred[found]].remove(found); - succ[found].add(pred[found]); + succ[pred[found]].erase(found); + succ[found].push_back(pred[found]); found = pred[found]; } return true; @@ -886,7 +891,7 @@ \begin{frame}[fragile]{Final algorithm} \begin{lstlisting} -void getMCBM(int n, List[] succ, boolean[] inU) { +void getMCBM(int n, vector>& succ, vector& inU) { while(findAndReverse(n, succ, inU)) {} //MCBM == edges from nodes in V (in succ) }\end{lstlisting}