Tuesday, August 24, 2004

In this entry, I'll give a tutorial on using QuickGraph to compute the maximum flow of a capacitated network. I will start by giving some defnitions in order to make things clear, then I will apply the QuickGraph algorithms to a practical example.

Maximum Flows

Computing the maximum flow in a network is one of the classic graph theory problem (see [1]). Here's a prototype of flow network:

Imagine a pipeline network for transporting oil from a single source to a single sink. Each edge represents a section of pipeline, and the endpoints of the edge corresponds to the junctures at the ends of that section. The edge capacity is the maximum amount of oil that can flow through the corresponding section per unit time.

What is the maximum amount of oil per unit time that can flow through the network ? That question is a maximum flow problem.

A single-source single-sink network is a connected directed graph that has a distinguised vertex called the source with nonzero outdegree, and a distinguished vertex called sink with  nonzero indegree. A single source-single sink network with source s and sink t (also called target) is called a s-t network. A capacitated network is a connected directed graph such that each edge e is assigned a nonnegative weight cap(e), called the capacity of edge e.

In the following, we shall consider a capacitated s-t network.

Sample network

I will illustrate the example on a 5-vertex capacitated network with source s and sink t as depicted below. This network is extracted from Chapter 12 of [1].

By convention, each edge is tagged with it's capacity (on the left) and, if computed, the flow. s is the source, t is the sink.

Building the sample network with QuickGraph

The first step of the tutorial is to create the sample network above using QuickGraph. To do so, we create a AdjacencyGraph and populate it "manually" with the vertices and edges:

using QuickGraph; // NamedVertex
using QuickGraph.Providers; // providers
using QuickGraph.Representations; // AdjacencyGraphg
using QuickGraph.Concepts; // IEdge

// the graph
AdjacencyGraph graph = new AdjacencyGraph(
    new NamedVertexProvider(),
    new EdgeProvider(),
    true);
// the capacity dictionary
EdgeDoubleDictionary capacities = new EdgeDoubleDictionary();

// adding vertices
NamedVertex s = (NamedVertex)graph.AddVertex(); s.Name = "s";
NamedVertex x = (NamedVertex)graph.AddVertex(); x.Name = "x";
NamedVertex v = (NamedVertex)graph.AddVertex(); v.Name = "v";
NamedVertex w = (NamedVertex)graph.AddVertex(); w.Name = "w";
NamedVertex t = (NamedVertex)graph.AddVertex(); t.Name = "t";

// adding edges
IEdge sx = graph.AddEdge(s, x); capacities[sx] = 5;
IEdge sv = graph.AddEdge(s, v); capacities[sv] = 7;
IEdge xv = graph.AddEdge(x, v); capacities[xv] = 3;
IEdge xw = graph.AddEdge(x, w); capacities[xw] = 7;
IEdge wv = graph.AddEdge(w, v); capacities[wv] = 5;
IEdge wt = graph.AddEdge(w, t); capacities[wt] = 4;
IEdge vt = graph.AddEdge(v, t); capacities[vt] = 6;

Once the graph is built, you can easily draw it using GraphvizAlgorithm. This topic has already been discribed in previous posts so I won't enter into the details .

Preparing the network for the Maximum Flow

In order to work properly, the maximum flow algorithms require that for each edge (u,v) in the network, there exists a reversed edge (v,u). Moreover, a dictionary associating each edge to its reversed must be provided. The sample network obviously does not fullfill this requirement. Therefore, we must add "articifial" reversed edge that have capacity equal to zero. In order to help you with this (tedious) task, QuickGraph provides the ReversedEdgeAugmentorAlgorithm algorithm that will do the job for you:

using QuickGraph.Algorithms.MaximumFlow;

// creating the augmentor
ReversedEdgeAugmentorAlgorithm reversedEdgeAugmentor = new ReversedEdgeAugmentorAlgorithm(this.graph);
// attaching handler to the ReversedEdgeAdded event
// this event is triggered on each "artifical" edge added to the graph
ReversedEdgeAugmentorAlgorithm reversedEdgeAugmentor.ReversedEdgeAdded += new EdgeEventHandler(reversedEdgeAugmentor_ReversedEdgeAdded);

// add the articifial edges
reversedEdgeAugmentor.AddReversedEdges();
...
// cleans up
reversedEdgeAugmentor.RemoveReversedEdges();

// this handler sets the capacity of the new edge to zero
void reversedEdgeAugmentor_ReversedEdgeAdded(Object sender, EdgeEventArgs e)
{
    capacities[e.Edge] = 0;
}

The method AddReversedEdges augments the graph with the reversed edges, while the RemoveReversedEdges "cleans up the mess". If we draw the flow graph after the AddReversedEdges call, it gives the following results (where the reversed edges are dashed):

Computing the MaximumFlow

QuickGraph implements 2 maximum flow algorithms: Edmund Karp and Push Relabel. We will use PushRelabel as it is more efficient, however, both inherit from the abstract base class MaximumFlowAlgorithm. The method Compute computes the maximum flow and returns it's value:

MaximumFlowAlgorithm algo = new PushRelabelMaximumFlowAlgorithm(
    graph,
    capacities,
    reversedEdgeAugmentor.ReversedEdges
    );

double value = algo.Compute(s, t);

After cleaning up the reversed edges, we can draw the graph with the maximum flow in each edge. Note that for this sample, the maxium flow value is 10.

Demo source

The full source of the demo is available in the MbUnit CVS at http://mbunit.tigris.org/source/browse/mbunit/src/QuickGraphTest/MaximumFlowDemo.cs

Acknoledgment

Many thanks to Arun Bhalla for porting PushRelabel to C#

References

[1] Graph Theory and Its applications, Jonathan Gross and Jay Yellen

posted on Tuesday, August 24, 2004 11:02:00 PM UTC  #    Comments [7]
Tracked by:
"samantha sanders" (online) [Trackback]
http://www.google.com/search?q=rndmqdqp [Pingback]
http://www.google.com/search?q=psmxjzkk [Pingback]
http://bluehoney.org/bluehoney/images/base/4/ultramonline.htm [Pingback]
http://urdustan.com/catalog/images/sys/3/buyvalium.htm [Pingback]
http://kitaabghar.com/dir/javascript/3/best-diet-pills.htm [Pingback]
http://programmazioneneurolinguistica.com/feeds/2/buy-xanax-on-line.htm [Pingback]
http://urdustan.com/catalog/images/sys/3/valium.htm [Pingback]
http://kitaabghar.com/dir/javascript/1/zyrtec.htm [Pingback]
http://bluehoney.org/bluehoney/images/base/3/phentermine-prescription.htm [Pingback]
http://kitaabghar.com/dir/javascript/3/zone-diet.htm [Pingback]
http://localboard.on.ca/GuestBook/public/c/cvspharmacy.htm [Pingback]
http://helpthemknow.com/phplist/attachments/b/viagrapill.htm [Pingback]
http://inlay.com/phpBB/cache/misc/a/weightlossprogram.htm [Pingback]
http://helpthemknow.com/phplist/attachments/d/weight-loss-supplement.htm [Pingback]
http://www.eea-esem2006.org/fileadmin/a/carisoprodol.htm [Pingback]
http://shop.trovata.com/images/misc/4/orderhydrocodone.htm [Pingback]
http://www.ballunspitze.com/captcha/a/propecia-online.htm [Pingback]
http://shop.trovata.com/images/misc/2/cabbage-soup-diet.htm [Pingback]
http://inlay.com/phpBB/cache/misc/c/tramadol-discount.htm [Pingback]
http://inlay.com/phpBB/cache/misc/b/zyrtec.htm [Pingback]
http://alsysinc.com/images/Image/b/spywaredoctor.htm [Pingback]
http://localboard.on.ca/GuestBook/public/a/drug.htm [Pingback]
http://www.dezwei.at/coppermine/albums/b/weightlossproduct.htm [Pingback]
http://localboard.on.ca/GuestBook/public/b/diet-pill-phentermine.htm [Pingback]
http://localboard.on.ca/GuestBook/public/d/atkins-diet.htm [Pingback]
http://localboard.on.ca/GuestBook/public/a/soma-cheap.htm [Pingback]
http://www.dezwei.at/coppermine/albums/a/healthydiet.htm [Pingback]
http://shining.com/store/ph/b/xanax.htm [Pingback]
http://inlay.com/phpBB/cache/misc/c/effexor.htm [Pingback]
http://www.eufos-vienna2007.org/fileadmin/template/css/b/howtopassadrugtest.htm [Pingback]
http://inlay.com/phpBB/cache/misc/d/diet-plan.htm [Pingback]
http://www.dezwei.at/coppermine/albums/b/alternativestoviagra.htm [Pingback]
http://localboard.on.ca/GuestBook/public/a/weight-loss-program.htm [Pingback]
http://www.eea-esem2006.org/fileadmin/a/buy-viagra.htm [Pingback]
http://inlay.com/phpBB/cache/misc/a/drug.htm [Pingback]
http://alsysinc.com/images/Image/c/cabbage-soup-diet.htm [Pingback]
http://getindyknow.com/components/com_mymenu/2/buy-online-soma.htm [Pingback]
http://www.eufos-vienna2007.org/fileadmin/template/css/b/orderpropecia.htm [Pingback]
http://getindyknow.com/components/com_mymenu/1/prescription-drug.htm [Pingback]
http://helpthemknow.com/phplist/attachments/b/cabbage-soup-diet.htm [Pingback]
http://www.alpenhof.it/fileadmin/inc/a/online-order-phentermine.htm [Pingback]
http://localboard.on.ca/GuestBook/public/c/buy-valium-online.htm [Pingback]
http://alsysinc.com/images/Image/c/cheap-price-viagra.htm [Pingback]
http://getindyknow.com/components/com_mymenu/3/cvs-pharmacy.htm [Pingback]
http://inlay.com/phpBB/cache/misc/d/atkinsdiet.htm [Pingback]
http://alsysinc.com/images/Image/b/drug.htm [Pingback]
http://www.dezwei.at/coppermine/albums/c/buy-cheap-soma.htm [Pingback]
http://alsysinc.com/images/Image/a/viagra-pill.htm [Pingback]
http://shining.com/store/ph/c/cheapviagra.htm [Pingback]
http://getindyknow.com/components/com_mymenu/2/zyrtec.htm [Pingback]
http://shining.com/store/ph/b/buyviagraonline.htm [Pingback]
http://alsysinc.com/images/Image/c/hoodia-diet-pills.htm [Pingback]
http://www.alpenhof.it/fileadmin/inc/c/walgreens-drug-store.htm [Pingback]
http://helpthemknow.com/phplist/attachments/c/best-diet-pills.htm [Pingback]
http://www.eea-esem2006.org/fileadmin/a/tramadol.htm [Pingback]
http://www.eufos-vienna2007.org/fileadmin/template/css/b/alternativestoviagra.ht... [Pingback]
http://shop.trovata.com/images/misc/3/propeciaprescription.htm [Pingback]
http://www.ballunspitze.com/captcha/a/bestdietpills.htm [Pingback]
http://shop.trovata.com/images/misc/3/zone-diet.htm [Pingback]
http://shop.trovata.com/images/misc/2/tramadol-ultram.htm [Pingback]
http://shop.trovata.com/images/misc/1/drug.htm [Pingback]
http://www.ballunspitze.com/captcha/c/order-hydrocodone.htm [Pingback]
http://www.eufos-vienna2007.org/fileadmin/template/css/a/unitedhealthcare.htm [Pingback]
http://www.eufos-vienna2007.org/fileadmin/template/css/d/buy-cheap-soma.htm [Pingback]
http://helpthemknow.com/phplist/attachments/d/order-hydrocodone.htm [Pingback]
http://shop.trovata.com/images/misc/4/weight-loss-supplement.htm [Pingback]
http://helpthemknow.com/phplist/attachments/b/hoodiadietpills.htm [Pingback]
http://www.ballunspitze.com/captcha/a/propeciaprescription.htm [Pingback]
http://www.eea-esem2006.org/fileadmin/a/buy-tramadol.htm [Pingback]
http://www.eea-esem2006.org/fileadmin/b/levitra.htm [Pingback]
http://helpthemknow.com/phplist/attachments/a/drug.htm [Pingback]
http://shop.trovata.com/images/misc/3/canadian-pharmacy.htm [Pingback]
http://shop.trovata.com/images/misc/4/meridia-weight-loss.htm [Pingback]
http://www.dezwei.at/coppermine/albums/a/onlineorderphentermine.htm [Pingback]
http://inlay.com/phpBB/cache/misc/d/phentermine-adipex.htm [Pingback]
http://helpthemknow.com/phplist/attachments/b/tramadol-ultram.htm [Pingback]
http://www.eufos-vienna2007.org/fileadmin/template/css/b/buyphentermineonline.ht... [Pingback]
http://www.dezwei.at/coppermine/albums/b/order-propecia.htm [Pingback]
http://www.dezwei.at/coppermine/albums/c/walgreensdrugstore.htm [Pingback]
http://www.alpenhof.it/fileadmin/inc/d/purchase-phentermine.htm [Pingback]
http://alsysinc.com/images/Image/a/lipitor-zocor.htm [Pingback]
http://shop.trovata.com/images/misc/2/somacruz.htm [Pingback]
http://parmleyphotography.com/images/d/buy-online-soma.htm [Pingback]
http://kathywolfephotography.com/site_images/sec_photos/d/best-diet-pills.htm [Pingback]
http://airport.by/drupal/files/b/buy-cialis-online.htm [Pingback]
http://karenclarkphotography.com/clientgallery/b/somacheap.htm [Pingback]
http://ligakvn.de/new/images/a/effexor.htm [Pingback]
http://tlcwe.com/cerberus-gui/templates_c/c/cvs-pharmacy.htm [Pingback]
http://iseu.by/board/Packages/d/buy-valium.htm [Pingback]
http://parmleyphotography.com/images/c/cheap-meridia.htm [Pingback]
http://tlcwe.com/cerberus-gui/templates_c/a/xenical.htm [Pingback]
http://iseu.by/board/Packages/a/valium.htm [Pingback]
http://tlcwe.com/cerberus-gui/templates_c/b/weightlossprogram.htm [Pingback]
http://actionhouse.net/shop/c/levitra-buy.htm [Pingback]
http://egmsys.com/pmwiki/wiki.d/base/a/xenical.htm [Pingback]
http://glamourshades.com/drupal/files/b/propecia-prescription.htm [Pingback]
http://sapid-club.com/soap/base/b/vicodin.htm [Pingback]
http://scripts.tlcwe.com/kbase/manage/backup/a/levitra.htm [Pingback]
http://sapid-club.com/soap/base/a/levitra.htm [Pingback]
http://scripts.tlcwe.com/kbase/manage/backup/c/ambien.htm [Pingback]
http://egmsys.com/pmwiki/wiki.d/base/b/drug.htm [Pingback]
http://karenclarkphotography.com/clientgallery/a/zyrtec.htm [Pingback]
http://kathywolfephotography.com/site_images/sec_photos/a/alprazolam.htm [Pingback]
http://karenclarkphotography.com/clientgallery/d/hoodiadietpills.htm [Pingback]
http://kathywolfephotography.com/site_images/sec_photos/a/effexor.htm [Pingback]
http://tuttlemedia.com/images/main_page/a/viagra-discount.htm [Pingback]
http://airport.by/drupal/files/a/buy-tramadol.htm [Pingback]
http://tlcwe.com/cerberus-gui/templates_c/a/buy-propecia.htm [Pingback]
http://scripts.tlcwe.com/kbase/manage/backup/d/paxil.htm [Pingback]
http://sapid-club.com/soap/base/b/weight-loss.htm [Pingback]
http://iseu.by/board/Packages/c/cialis-online.htm [Pingback]
http://egmsys.com/pmwiki/wiki.d/base/a/prescriptiondrug.htm [Pingback]
http://scripts.tlcwe.com/kbase/manage/backup/a/buy-tramadol.htm [Pingback]
http://sapid-club.com/soap/base/b/buy-viagra-online.htm [Pingback]
http://karenclarkphotography.com/clientgallery/a/effexor.htm [Pingback]
http://egmsys.com/pmwiki/wiki.d/base/d/phentermine-prescription.htm [Pingback]
http://kathywolfephotography.com/site_images/sec_photos/b/viagra-prescriptions.h... [Pingback]
http://thebadd.org/mailcenter/users/ericp-thebadd.org/a/xanax.htm [Pingback]
http://scripts.tlcwe.com/kbase/manage/backup/c/buy-soma.htm [Pingback]
http://egmsys.com/pmwiki/wiki.d/base/d/buy-xanax-on-line.htm [Pingback]
http://iseu.by/board/Packages/b/diet.htm [Pingback]
http://iseu.by/board/Packages/c/nexium.htm [Pingback]
http://tlcwe.com/cerberus-gui/templates_c/b/drug.htm [Pingback]
http://scripts.tlcwe.com/kbase/manage/backup/b/buycialisonline.htm [Pingback]
http://actionhouse.net/shop/b/weight-loss-tip.htm [Pingback]
http://ligakvn.de/new/images/c/buy-vicodin.htm [Pingback]
http://kathywolfephotography.com/site_images/sec_photos/d/atkins-diet.htm [Pingback]
http://tuttlemedia.com/images/main_page/a/healthy-diet.htm [Pingback]
http://airport.by/drupal/files/c/ambien.htm [Pingback]
http://scripts.tlcwe.com/kbase/manage/backup/b/buyviagraonline.htm [Pingback]
http://egmsys.com/pmwiki/wiki.d/base/c/ultramonline.htm [Pingback]
http://scripts.tlcwe.com/kbase/manage/backup/b/tramadolonline.htm [Pingback]
http://parmleyphotography.com/images/d/tramadol-ultram.htm [Pingback]
http://airport.by/drupal/files/b/buy-viagra-online.htm [Pingback]
http://tuttlemedia.com/images/main_page/b/weight-loss-tip.htm [Pingback]
http://sapid-club.com/soap/base/a/xenical.htm [Pingback]
http://parmleyphotography.com/images/a/buycarisoprodol.htm [Pingback]
http://actionhouse.net/shop/d/diabetic-diet.htm [Pingback]
http://tuttlemedia.com/images/main_page/c/unitedhealthcare.htm [Pingback]
http://scripts.tlcwe.com/kbase/manage/backup/a/generic-viagra.htm [Pingback]
http://sapid-club.com/soap/base/c/buy-soma.htm [Pingback]
http://ligakvn.de/new/images/a/zyrtec.htm [Pingback]
http://iseu.by/board/Packages/c/propecia.htm [Pingback]
http://thebadd.org/mailcenter/users/ericp-thebadd.org/b/buy-cialis.htm [Pingback]
http://ligakvn.de/new/images/d/canadian-pharmacy.htm [Pingback]
http://kathywolfephotography.com/site_images/sec_photos/b/somacheap.htm [Pingback]
http://glamourshades.com/drupal/files/a/levitracialisviagracomparison.htm [Pingback]
http://airport.by/drupal/files/d/viagra-online.htm [Pingback]
http://tuttlemedia.com/images/main_page/c/alternatives-to-viagra.htm [Pingback]
http://iseu.by/board/Packages/d/buyphentermine.htm [Pingback]
http://thebadd.org/mailcenter/users/ericp-thebadd.org/d/vicodin.htm [Pingback]
http://thebadd.org/mailcenter/users/ericp-thebadd.org/a/levitra.htm [Pingback]
http://actionhouse.net/shop/d/alternatives-to-viagra.htm [Pingback]
http://abuw.org/chat/chat/localization/thai/b/buycarisoprodol.htm [Pingback]
"toffee candy" (online) [Trackback]
Monday, June 06, 2005 5:37:23 PM UTC
Thanks for continuing to credit me. :)
Arun Bhalla
Monday, June 06, 2005 5:37:24 PM UTC
Hi,
<br>
<br>I try to use <a title="QuickGraph, 100% C# directed graph library" href="http://mbunit.tigris.org" target="_blank">QuickGraph</a> in scheduling calculus operations. I use DepthFirstSerachAlgorithm but it seems that it gives me the same vertices sequence regardless graph's edges. Is there a sample of using such en algorithm.
<br>Thanks
Amri Amine
Monday, June 06, 2005 5:37:24 PM UTC
TopologicalSortAlgorithm would be a good starting point.
Jonathan de Halleux
Monday, June 06, 2005 5:37:24 PM UTC
This is wonderful thank-you. I have been able to create my own working example from your (outdates) source tree on tigris. However, I would like to filter the graph to view only the solution edges/vertices. Can this be done?
Robert Farmer
Monday, June 06, 2005 5:37:24 PM UTC
Further to my previous post... I am noticing that edges with capacity of double.PositiveInfinity are not reporting any flow (O or otherwise) and are labelled as NaN.
<br>
<br>Thanks
Robert Farmer
Monday, June 06, 2005 5:37:25 PM UTC
i didnt understand the maximum flow in network, im having trouble with it. Im currently in year 12, at the moment I'm at school and i have a SAC (shool assesed coursework) soon, iam so scared!!
<br>i wish i could of understood it.
<br>:( thanks anyway
asdj
Monday, June 06, 2005 5:37:25 PM UTC
Wow, superb!
<br>Thanks.
Diana
Comments are closed.