Max Flow

Max flow very simply finds the maximum flow which can be pushed from a single source to a single drain though some random network of connections. The following algorithm uses djikstra's with an adjacency matrix to find the maximum flow path. The running time is approximately O(v[e+v log v]), where e is the number of edges and v is the number of vertices.

Note that this program works for a maximum of 100 nodes. Change the size of the array to allow it to do more; it can do up to about 2000 before running out of memory.

C++ Source Code - Written for Internet Bandwidth
int max1; int prev [102]; int flow [102]; int matrix [102][102]; struct anInt {        int x; }; bool operator&lt;(const anInt &a, const anInt &b) {return flow[a.x] &lt; flow[b.x];} using namespace std; int maxPath (int a, int b) { priority_queue &lt;anInt&gt; PQ; for (int i = 0; i &lt; max1+2; i++) flow[i] = 0; anInt curr, next; flow[a] = 10000000; curr.x = a;    PQ.push(curr); while (!PQ.empty) {        curr = PQ.top; PQ.pop; ;     for (int i = 0; i &lt; max1+2; i++) {            if ( min(flow[curr.x], matrix[curr.x][i]) &gt; flow[i]) {                flow[i] = min(flow[curr.x], matrix[curr.x][i]); prev[i] = curr.x;                next.x = i;                 PQ.push(next); }        }     }     return flow[b]; } int main {    int count = 0; cin &gt;&gt; max1; while (max1 &gt; 0) {        count ++; for(int i = 0; i &lt; max1+2; i++) for (int j = 0; j &lt; max1+2; j++) matrix[i][j] = 0; int sn, en, co; cin &gt;&gt; sn &gt;&gt; en &gt;&gt; co; matrix[0][sn] = 10000000; matrix[en][max1+1] = 10000000; for (int i = 0; i &lt; co; i++) {            int sx, ex, fl; cin &gt;&gt; sx &gt;&gt; ex &gt;&gt; fl; matrix[sx][ex] += fl; matrix[ex][sx] += fl; }        int path = maxPath(0, max1+1); int ans = 0; while (path != 0) {            int t2 = max1+1; int t1 = prev[t2]; while (t1 != 0) {                matrix[t1][t2] -= path; matrix[t2][t1] += path; t2 = t1; t1 = prev[t2]; }            ans += path; path = maxPath(0, max1+1); }        cout &lt;&lt; "Network " &lt;&lt; count &lt;&lt; endl &lt;&lt; "The bandwidth is " &lt;&lt; ans &lt;&lt; "." &lt;&lt; endl &lt;&lt; endl; cin &gt;&gt; max1; }    return 0; }
 * 1) include &lt;iostream&gt;
 * 2) include &lt;algorithm&gt;
 * 3) include &lt;queue&gt;

Java
public class Main { 	static int n;		//Number of vertices (including source and sink) static int s,t;		//Indices of source and sink static int[][] a;	//a[i][j] = capacity on edge (i,j) public static void main(String args[]) { 		//Flow int rtn = 0; while(true) { 			//BFS //p[i] = parent of i in BFS. //p[i] = -1 if unsearched, or -2 if root int[] p = new int[n]; Arrays.fill(p, -1); p[s] = -2; Queue q = new LinkedList; q.offer(s); while(!q.isEmpty) { 				int cur = q.poll; for(int i=0;i < n;i++) { 					if(a[cur][i] > 0 && p[i] == -1) { 						p[i] = cur; q.offer(i); } 				} 			} 			//Break if no path is found if(p[t] == -1) break; //Get path capacity int cap = 999999999; int cur = t; 			while(p[cur] != -2) { 				cap = Math.min(cap, a[p[cur]][cur]); cur = p[cur]; } 			rtn += cap; //Update cur = t; 			while(p[cur] != -2) { 				a[p[cur]][cur] -= cap; a[cur][p[cur]] += cap; cur = p[cur]; } 		} 	} }

UVA Problems
820 - Internet Bandwidth