0

I'm attempting to figure out how to program in a method that will find the max-weight, M(u, v), of a path between two vertices, u and v. Below is how my graph represented in an adjacency list is set up.

public class Graph {
int size;
List<Edge> Graph[];

/**
 * Creates graph of certain size by creating a list of each vertex and then attaching a link list to each
 * vertex for edges
 * @param size - number of vertices
 */
public Graph(int size) {
    this.size = size;
    Graph = new LinkedList[size];
    for(int i = 0; i < size; i++)
    {
        Graph[i] = new LinkedList<Edge>();
    }
}

/**
 * Adds an edge to the graph by placing an edge object in the link list attached to the index that 
 * corresponds to the starting vertex of the edge
 * @param u - starting vertex
 * @param v - terminal vertex
 * @param w - weight of edge
 */
public void add(int u, int v, int w) {
    Graph[u].add(0, new Edge(v, w));
}

/**
 * Determines whether or not there is an edge between two vertices
 * @param u - starting vertex
 * @param v - terminal vertex
 * @return
 */
public boolean hasEdge(int u, int v) {
    for(Edge i : Graph[u])
        if(i.getTerminalVertex() == v)
            return true;
    return false;
}

/**
 * Determines the maximum path weight between two vertices
 * @param u - starting vertex
 * @param v - terminal vertex
 * @return
 */
public int M(int u, int v) {
    int max = 0;
    int current = 0;
    for(Edge e : Graph[u])
    {
        if(v == e.getTerminalVertex())
        {

        }
        if(u == v)
        {
            return max;
        }
        if(hasEdge(u, e.getTerminalVertex()))
        {

            current = e.getEdgeWeight() + M(e.getTerminalVertex(), v);
        }
        if(current > max)
        {
            max = current;
        }
    }
    return max;
}

Despite my best efforts, I can not for the life of me figure out the best way of accomplishing this. If anyone is able to provide some assistants, it would be much appreciated.

  • if there are 10 paths from `u` to `v`, would max edge of any path suffice? or do you need max edge of all possible paths? Besides, is preprocessing on graph allowed? – Shihab Shahriar Khan Dec 10 '17 at 17:16
  • Are you trying to solve the [longest path problem](https://en.wikipedia.org/wiki/Longest_path_problem)? Perhaps https://stackoverflow.com/questions/8052826/longest-path-in-graph will be of some use. – Jim Mischel Dec 10 '17 at 20:01

0 Answers0