What's an intelligent way of creating a graph like object for the Ford-Fulkerson algorithm?

StackOverflow https://stackoverflow.com/questions/20543521

  •  31-08-2022
  •  | 
  •  

Question

I'm trying to implement a Ford–Fulkerson algorithm in java and I've been having some problems where my code gets obnoxiously and unnecessarily complicated.

What I do want is to have:

class Node:
    private int id
    private static int idAssigner // I may move this to another class
    // etc

class flowNetwork
    private Node source  // begin point
    private Node sink    // end point

Now I want to group nodes similarly how I would a (bidirectional) tree. Each node has a list of all nodes it's connected to.

My problem is this: How could I give this connection a value (maximum flow, current flow) ?

Should I make another class Connection that has Node A Node B and max flow / current flow. And if I do that, how should I connect the nodes ? (as in should every node have a Connection and wouldn't that be redundant ? I'm a bit stuck. edit Or should I just have Connections and implement some sort of search function to acomodate linking elements. It's all I can think of to be honest.

P.S.

This class is mostly just the math part, so I have never implemented a graph, nor does the course cover this, so thank you for helping a novice :) (that's if this doesn't get closed in like 5 minutes).

Was it helpful?

Solution

I think, you can use map of linked nodes in each node. With node key and link information as value.

It's not a fast solution, but it's simple.

Faster will be to have a matrix, elements of wich is a link objects, containing all link info. Rows and columns will be node indices.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top