Floyd's shortest path for undirected graph - java
I have implemented an program that can calculate shortest path's for any graph either by using Floyd's/Dijkstra's algorithm based on user input.
Both algorithm's work fine for directed graph.
The output is supposed to display
1)The actual path to be taken from the start vertex
2)The shortest distance to be travelled
But when it come's to undirected graph I run into problems.
My graph is not represented by an adjacency matrix but with an Graph & Edge classes as shown below
class Graph
{
private final int noOfVertices;
private final ArrayList<Edge> edges = new ArrayList<Edge>();
private boolean undirected = false;
Graph(int vertexCount)
{
noOfVertices = vertexCount;
edges.ensureCapacity(2 * (vertexCount - 1));
}
public int getWeight(int src,int dst)
{
int weight=90000;
for(Edge edge:edges)
{
if(edge.src==src && edge.dst==dst)
{
weight=edge.weight;
break;
}
}
return weight;
}
public int getEdgeCount(){return edges.size();}
public int getVertexCount(){return noOfVertices;}
public static class Edge
{
public int src;
public int dst;
public int weight;
Edge(int v1, int v2, int w)
{
src = v1;
dst = v2;
weight = w;
}
}
}
To implement undirected graph this is the code used below
void newEdge(int src,int dst,int weight)
{
edges.add(new Edge(src,dst,weight));
if(undirected){ edges.add(new Edge(dst,src,weight));}
}
Now Dijkstra's algorithm work's fine on this setup but when I use Floyd's algorithm I start to get incorrect path's but correct distance's
This is my Floyd's algorithm
public static Integer[] Floyd(Graph graph, int startVertex, int endVertex)
{
ArrayList<Integer> pathInfo = new ArrayList<Integer>();
int dist[][] = new int[graph.getVertexCount()][graph.getVertexCount()];
int path[][] = new int[graph.getVertexCount()][graph.getVertexCount()];
int V = graph.getVertexCount();
for (int i = 0; i < V; i++)
{
for (int j = 0; j < V; j++)
{
if (i == j)
{
dist[i][j] = 0;
}
else
{
dist[i][j] = graph.getWeight(i, j);
}/*Initialize with edge weight's between vertices i,j.If edge does not exist graph.getWeight() return's 90000 i.e simply an value somewhat close to infinite because when I use Integer.MAX_VALUE I get negative distance's when doing dist[i][k]+dist[k][j] so i avoid it*/
path[i][j] = j;
}
}
/*actual Floyd's algorithm*/
for (int k = 0; k < V; k++)
{
for (int i = 0; i < V; i++)
{
for (int j = 0; j < V; j++)
{
if (dist[i][j] > dist[i][k] + dist[k][j])
{
dist[i][j] = dist[i][k] + dist[k][j];
path[i][j] = k; /*if path exist's record the intermediate vertex*/
}
}
}
}
int currentVertex=endVertex; /*Start from last vertex*/
int nextVertex=path[startVertex][endVertex];/*Find path*/
pathInfo.add(currentVertex);/*Add Current Vertex*/
while(currentVertex!=nextVertex)/*Backtrack until the vertex we are currently at and the next vertex we need to go to from there are the same which mean's we have reached our target*/
{
currentVertex=nextVertex;
pathInfo.add(0,currentVertex);
nextVertex=path[startVertex][nextVertex];
}
pathInfo.add(0,startVertex);/*Finally add the vertex we ended at*/
pathInfo.add(dist[startVertex][endVertex]);/*Come's out correct in both cases*/
return pathInfo.toArray(new Integer[]{});/*Convert info to array*/
}
So here is my undirected graph given below.
The doted lines represent's an edge that goes in both direction's.
0--1--2--3
Each edge has an weight of 2
Now when I call Floyd's algorithm with start vertex=0 and end vertex=3
I get The correct output path.
0,1,2,3
But when I again call Floyd's algorithm with start Vertex=3 and end Vertex=0
The output path is
3,2,0
Vertex 1 is missing.
But with Dijkstra's algorithm I get the correct result's in both cases
Here is the path matrix calculated above.
0 1 1 2
0 1 2 2
1 1 2 3
2 2 2 3
The distance come's out correct that is 6 in both cases but the path is wrong when I reverse the vertex order only for Floyd's algorithm.
Most of the video ideas were incorporated from this link
https://www.bing.com/videos/search?q=floyd%27s+algorithm&&view=detail&mid=E17F409B3AB0B2307233E17F409B3AB0B2307233&&FORM=VRDGAR
Any idea's on where I went wrong?
Related
Implementing Union-Find Algorithm for Kruskal's Algorithm to find Minimum Spanning Tree in Java
I am trying to solve the following Leetcode problem (https://leetcode.com/problems/connecting-cities-with-minimum-cost), and my approach is to figure out the total weight of the minimum spanning tree (MST) from the input graph using Kruskal's Algorithm using the Union-Find data structure. However, my code online passes 51/63 of the test cases, returning the incorrect result on the following test case, which is too hard to debug, since the input graph is too large. 50 [[2,1,22135],[3,1,13746],[4,3,37060],[5,2,48513],[6,3,49607],[7,1,97197],[8,2,95909],[9,2,82668],[10,2,48372],[11,4,17775],[12,2,6017],[13,1,51409],[14,2,12884],[15,7,98902],[16,14,52361],[17,8,11588],[18,12,86814],[19,17,49581],[20,4,41808],[21,11,77039],[22,10,80279],[23,16,61659],[24,12,89390],[25,24,10042],[26,12,78278],[27,15,30756],[28,6,2883],[29,8,3478],[30,7,29321],[31,12,47542],[32,20,35806],[33,3,26531],[34,12,16321],[35,27,82484],[36,7,55920],[37,24,21253],[38,23,90537],[39,7,83795],[40,36,70353],[41,34,76983],[42,14,63416],[43,15,39590],[44,9,86794],[45,3,31968],[46,19,32695],[47,17,40287],[48,1,27993],[49,12,86349],[50,11,52080],[17,27,65829],[42,45,87517],[14,23,96130],[5,50,3601],[10,17,2017],[26,44,4118],[26,29,93146],[1,9,56934],[22,43,5984],[3,22,13404],[13,28,66475],[11,14,93296],[16,44,71637],[7,37,88398],[7,29,56056],[2,34,79170],[40,44,55496],[35,46,14494],[32,34,25143],[28,36,59961],[10,49,58317],[8,38,33783],[8,28,19762],[34,41,69590],[27,37,26831],[15,23,53060],[5,11,7570],[20,42,98814],[18,34,96014],[13,43,94702],[1,46,18873],[44,45,43666],[22,40,69729],[4,25,28548],[8,46,19305],[15,22,39749],[33,48,43826],[14,15,38867],[13,22,56073],[3,46,51377],[13,15,73530],[6,36,67511],[27,38,76774],[6,21,21673],[28,49,72219],[40,50,9568],[31,37,66173],[14,29,93641],[4,40,87301],[18,46,41318],[2,8,25717],[1,7,3006],[9,22,85003],[14,45,33961],[18,28,56248],[1,31,10007],[3,24,23971],[6,28,24448],[35,39,87474],[10,50,3371],[7,18,26351],[19,41,86238],[3,8,73207],[11,34,75438],[3,47,35394],[27,32,69991],[6,40,87955],[2,18,85693],[5,37,50456],[8,20,59182],[16,38,58363],[9,39,58494],[39,43,73017],[10,15,88526],[16,23,48361],[4,28,59995],[2,3,66426],[6,17,29387],[15,38,80738],[12,43,63014],[9,11,90635],[12,20,36051],[13,25,1515],[32,40,72665],[10,40,85644],[13,40,70642],[12,24,88771],[14,46,79583],[30,49,45432],[21,34,95097],[25,48,96934],[2,35,79611],[9,26,71147],[11,37,57109],[35,36,67266],[42,43,15913],[3,30,44704],[4,32,46266],[5,10,94508],[31,39,45742],[12,25,56618],[10,45,79396],[15,28,78005],[19,32,94010],[36,46,4417],[6,35,7762],[10,13,12161],[49,50,60013],[20,23,6891],[9,50,63893],[35,43,74832],[10,24,3562],[6,8,47831],[29,32,82689],[7,47,71961],[14,41,82402],[20,33,38732],[16,26,24131],[17,34,96267],[21,46,81067],[19,47,41426],[13,24,68768],[1,25,78243],[2,27,77645],[11,25,96335],[31,45,30726],[43,44,34801],[3,42,22953],[12,23,34898],[37,43,32324],[18,44,18539],[8,13,59737],[28,37,67994],[13,14,25013],[22,41,25671],[1,6,57657],[8,11,83932],[42,48,24122],[4,15,851],[9,29,70508],[7,32,53629],[3,4,34945],[2,32,64478],[7,30,75022],[14,19,55721],[20,22,84838],[22,25,6103],[8,49,11497],[11,32,22278],[35,44,56616],[12,49,18681],[18,43,56358],[24,43,13360],[24,47,59846],[28,43,36311],[17,25,63309],[1,14,30207],[39,48,22241],[13,26,94146],[4,33,62994],[40,48,32450],[8,19,8063],[20,29,56772],[10,27,21224],[24,30,40328],[44,46,48426],[22,45,39752],[6,43,96892],[2,30,73566],[26,36,43360],[34,36,51956],[18,20,5710],[7,22,72496],[3,39,9207],[15,30,39474],[11,35,82661],[12,50,84860],[14,26,25992],[16,39,33166],[25,41,11721],[19,40,68623],[27,28,98119],[19,43,3644],[8,16,84611],[33,42,52972],[29,36,60307],[9,36,44224],[9,48,89857],[25,26,21705],[29,33,12562],[5,34,32209],[9,16,26285],[22,37,80956],[18,35,51968],[37,49,36399],[18,42,37774],[1,30,24687],[23,43,55470],[6,47,69677],[21,39,6826],[15,24,38561]] I'm having trouble understanding why my code will fail a test case, since I believe I am implementing the steps of Kruskal's Algorithm propertly: Sorting the connections in increasing order of weight. Building the MST by going through each connection in the sorted list and selecting that connection if it does not result in a cycle in the MST. Below is my Java code: class UnionFind { // parents[i] = parent node of node i. // If a node is the root node of a component, we define its parent // to be itself. int[] parents; public UnionFind(int n) { this.parents = new int[n]; for (int i = 0; i < n; i++) { this.parents[i] = i; } } // Merges two nodes into the same component. public void union(int node1, int node2) { int node1Component = find(node1); int node2Component = find(node2); this.parents[node1Component] = node2Component; } // Returns the component that a node is in. public int find(int node) { while (this.parents[node] != node) { node = this.parents[node]; } return node; } } class Solution { public int minimumCost(int n, int[][] connections) { UnionFind uf = new UnionFind(n + 1); // Sort edges by increasing cost. Arrays.sort(connections, new Comparator<int[]>() { #Override public int compare(final int[] a1, final int[] a2) { return a1[2] - a2[2]; } }); int edgeCount = 0; int connectionIndex = 0; int weight = 0; // Greedy algorithm: Choose the edge with the smallest weight // which does not form a cycle. We know that an edge between // two nodes will result in a cycle if those nodes are already // in the same component. for (int i = 0; i < connections.length; i++) { int[] connection = connections[i]; int nodeAComponent = uf.find(connection[0]); int nodeBComponent = uf.find(connection[1]); if (nodeAComponent != nodeBComponent) { weight += connection[2]; edgeCount++; } if (edgeCount == n - 1) { break; } } // MST, by definition, must have (n - 1) edges. if (edgeCount == n - 1) { return weight; } return -1; } }
As #geobreze stated, I forgot to unite the components (disjoint sets) of node A and node B. Below is the corrected code: if (nodeAComponent != nodeBComponent) { uf.union(nodeAComponent, nodeBComponent); weight += connection[2]; edgeCount++; }
Creating an array of distances from a starting vertex in a graph in java
I'm having trouble implementing a method to create an array of distances using BFS from a chosen starting vertex, it currently seems to work in some cases, but fails in larger graphs. The idea is each index in the array represents the corresponding vertex in a graph. here is the relevant code public int[] getDistances(Graph g, int startVertex) { boolean[] visited = new boolean[g.getNumberOfVertices()]; int[] distance = new int[g.getNumberOfVertices()]; for (int i = 0; i < distance.length; i++) { distance[i] = -1; } ArrayList<Integer> q = new ArrayList<Integer>(); q.add(startVertex); int dist_int = 0; while (!q.isEmpty()) { int current = q.get(0); q.remove(0); dist_int++; visited[startVertex] = true; for (int j = 0; j < g.getEdgeMatrix()[current].length; j++) { if (startVertex == j) distance[j] = 0; if (g.getEdgeMatrix()[current][j] == 1 && !visited[j]) { q.add(j); distance[j] = dist_int; visited[j] = true; } } } return distance; } the idea is it iterates through an adjacency matrix, determining each unvisited child, each time a child is found the current dist_int is assigned to the corresponding index in the distance array. With the distance increasing each time all of the children of the current node have been assigned, then the current moves to the first child and repeats.
Instead of using dist_int to hold the distance values just call distance[j] = distance[current] + 1;
Determining Whether Graph G = (V,E) Contains a Negative-Weight Cycle
In this program, I am given an input text file that gives information about a weighted, directed graph G = (V, E, w) The first line of the text file in input stores V (the number of vertices) and E (the number of edges). The following lines store data about edges (u, v) in order u, v, weight. I'm trying to implement a code that considers this input and determines whether G contains a negative-weight cycle. So far, I've tried to use the Bellman Ford algorithm to try to get this to work: I started by initializing a dist[] array that initializes distances from the source to all other vertices as some really high number (making sure src to src is 0). Next, I relax all edges |V| - 1 times. Finally, I check for negative-weight cycles by iterating through the array of edges again, checking to see if we get a shorter path. However, when I try to do that second step of relaxing the edges, I keep getting an index out of bounds error. NOTE: To examine the code below, just scroll down to the method isNegativeCycle(). I just included some of the other stuff in case anyone needs background information. public class P1 { //instance variables static int V; //number of vertices static int E; //number of edges //vertex class public class Vertex { int ID; //the name of the vertex } //edge class public class Edge { Vertex source; //the source vertex - its a directed graph Vertex dest; //the destination vertex int weight; //the weight of the edge } //graph class where all the magic happens public class Graph { //Each graph has an array of edges Edge edgearray[]; //constructor public Graph(int n, int m) { V = n; E = m; edgearray = new Edge[E]; for (int i = 0; i < E; i++) { edgearray[i] = new Edge(); } } //THIS IS THE IMPORTANT METHOD public String isNegativeCycle(Graph G, int src) { int dist[] = new int[V]; Arrays.fill(dist, Integer.MAX_VALUE); dist[src] = 0; //cos the distance from A to A is 0 //Relax all edges for (int i = 1; i <= V-1; i++) { for (int j = 0; j < E; j++) { int u = G.edgearray[j].source.ID; int v = G.edgearray[j].dest.ID; int weight = G.edgearray[j].weight; //THIS IS WHERE I GET THE INDEX OUT OF BOUNDS ERROR if (dist[u]!= Integer.MAX_VALUE && (dist[u]+weight) < dist[v]) { dist[v] = dist[u]+weight; } } //check for a negative cycle for (int a = 0; a < E; a++) { int u = G.edgearray[a].source.ID; int v = G.edgearray[a].dest.ID; double weight = G.edgearray[a].weight; if (dist[u] != Integer.MAX_VALUE && dist[u]+weight < dist[v]) { return "YES"; } } return "NO"; } }//end of graph class //main method public static void main(String[] args) { P1 instance = new P1(); int n; int m; int counter = 0; boolean fl = true; String infileName = args[0]; Graph G = instance.new Graph(V, E); File infile = new File(infileName); Scanner fileReader = null; try { fileReader = new Scanner(infile); while (fileReader.hasNextLine()) { //if we're reading the first line if (fl == true) { String[] temp = fileReader.nextLine().split(" "); n = Integer.parseInt(temp[0]); V = n; m = Integer.parseInt(temp[1]); E = m; G = instance.new Graph(V, E); fl = false; } //if we're reading any line other than the first line else { String[] temp = fileReader.nextLine().split(" "); //G.addEdge(temp[0], temp[1], Double.parseDouble(temp[2])); Vertex newsrc = instance.new Vertex(); Vertex newdest = instance.new Vertex(); newsrc.ID = Integer.parseInt(temp[0]); newdest.ID = Integer.parseInt(temp[1]); Edge newEdge = instance.new Edge(); newEdge.source = newsrc; newEdge.dest = newdest; newEdge.weight = Integer.parseInt(temp[2]); G.edgearray[counter] = newEdge; counter++; } } } catch (FileNotFoundException e) { System.out.println("File not found."); } System.out.println(G.isNegativeCycle(G, 0)); } } My current input file doesn't really matter at this point, but after this code runs, I expect the output to be "YES." Thank you!
I should've included my input file. In my input file, you would see that the vertex names start at 1. Thus, when calling isNegativeCycle, I shoudl've sent in a 1 instead of a 0. In addition, I make the dist[] array one size bigger.
Generate Random Symmetric Weighted Adjacency Matrix
I'm trying to test implementations of Prim's and Kruskal's algorithm by using a cost adjacency matrix. I'm generating these matrices on the amount of vertices in the graph, and the amount of edges in the graph. It does not have to be a connected graph. Here's what I have so far: final static int infinity = 2000000000; public static int[][] genAdjMat(int V, int E) { int[][] a = new int[V][V]; int e = E; for(int i = 0; i < V; i++) { for(int j = i; j < V; j++) { if(i == j) { a[i][j] = 0; } else { if(Math.random() < 0.5 && e >= 0) { int temp = (int)Math.ceil(Math.random()*e); a[i][j] = temp; a[j][i] = temp; e--; } else { a[i][j] = infinity; a[j][i] = infinity; } } } } return a; } Right now, it generates a symmetric array but it doesn't use all the edges that I specify. I'm having trouble figuring out how to use up all the edges and still have them randomly placed throughout the matrix while maintaining symmetry.
I'd suggest the following: Generate the list of all possible undirected edges (V * (V - 1) / 2 items). Shuffle it. Pick the first E edges.
Straight forward: just generate the edges independently. Random random = new Random(); Set<Map.Entry<Integer,Integer>> edges = Sets.newHashSet(); for (int i=0; i<e; i++) { do { int xCoordinate = random.nextInt(V); int yCoordinate = random.nextInt(V); } while(!edges.add(xCoordinate, yCoordinate)); } Now use the edges to put them in your matrix. Alternatively (while iterating over the matrix), use the following probability function: p(A[i,j] == 1) = (e - k) / (V^2 - (i * V + j)). Where k is the number of already assigned edges. The point of this is - have a probability less then 1 while the number of entries that are remaining is still higher then the number of edges you have to assign, this becomes 1 when the number of edges you still have to assign is equal to remaining entries to iterate over.
Bellman Ford Display predecessor for every iterations [closed]
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 9 years ago. Here the predecessor vertices get displayed for all iterations. I want the final predecessor to be displayed for the vertices import java.io.*; import java.util.*; public class BellmanFord { LinkedList<Edge> edges; int d[]; int n,e,s; final int INFINITY=999; private static class Edge { int u,v,w; public Edge(int a, int b, int c){ u=a; v=b; w=c; } BellmanFord() throws IOException{ int item; edges=new LinkedList<Edge>(); BufferedReader inp = new BufferedReader (new InputStreamReader(System.in)); System.out.print("Enter number of vertices "); n=Integer.parseInt(inp.readLine()); System.out.println("Cost Matrix"); for(int i=0;i<n;i++) for(int j=0;j<n;j++){ item=Integer.parseInt(inp.readLine()); if(item!=0) edges.add(new Edge(i,j,item)); } e=edges.size(); d=new int[n]; System.out.print("Enter the source vertex "); s=Integer.parseInt(inp.readLine()); } void relax() { int i,j; for(i=0;i<n;++i) d[i]=INFINITY;; d[s] = 0; for (i = 0; i < n - 1; ++i) for (j = 0; j < e; ++j) if (d[edges.get(j).u] + edges.get(j).w < d[edges.get(j).v]) { d[edges.get(j).v] = d[edges.get(j).u] + edges.get(j).w; /*Gives me the predecessor nodes of all iterations How can i get the final predecessornodes*/ System.out.println(edges.get(j).v+" Has predecessor " + edges.get(j).u); } } boolean cycle() { int j; for (j = 0; j < e; ++j) if (d[edges.get(j).u] + edges.get(j).w < d[edges.get(j).v]) return false; return true; } public static void main(String args[]) throws IOException { BellmanFord r=new BellmanFord(); r.relax(); if(r.cycle()) for(int i=0;i<r.n;i++) System.out.println(r.s+"to"+i+" ==> "+r.d[i]); else System.out.println(" There is a nagative edge cycle "); } } The erronous output is as follows. I am trying to print out the predecessor for every iterations: **OUTPUT:** Enter number of vertices Cost Matrix 0 -1 4 0 0 0 0 3 2 2 0 0 0 0 0 0 1 5 0 0 0 0 0 -3 0 Enter the source vertex 1 Has predecessor 0 2 Has predecessor 0 2 Has predecessor 1 3 Has predecessor 1 4 Has predecessor 1 3 Has predecessor 4 0to0 ==> 0 0to1 ==> -1 0to2 ==> 2 0to3 ==> -2 0to4 ==> 1
Seems that your last lines are very less understandable and so I give you my program which I made a few days back.. you can look for the mistake your program makes since its difficult to understand 100 lines of code and find out errors :Also I advice you to focus more on writing neat and commented codes rather than straightaway focussing on time optimizations. Only check the logic and try to implement it in your code ,that's why I didn't post a Java code so that you could get everything easily :) // A C / C++ program for Bellman-Ford's single source shortest path algorithm. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> // a structure to represent a weighted edge in graph struct Edge { int src, dest, weight; }; // a structure to represent a connected, directed and weighted graph struct Graph { // V-> Number of vertices, E-> Number of edges int V, E; // graph is represented as an array of edges. struct Edge* edge; }; // Creates a graph with V vertices and E edges struct Graph* createGraph(int V, int E) { struct Graph* graph = (struct Graph*) malloc( sizeof(struct Graph) ); graph->V = V; graph->E = E; graph->edge = (struct Edge*) malloc( graph->E * sizeof( struct Edge ) ); return graph; } // A utility function used to print the solution void printArr(int dist[], int n) { printf("Vertex Distance from Source\n"); for (int i = 0; i < n; ++i) printf("%d \t\t %d\n", i, dist[i]); } // The main function that finds shortest distances from src to all other // vertices using Bellman-Ford algorithm. The function also detects negative // weight cycle void BellmanFord(struct Graph* graph, int src) { int V = graph->V; int E = graph->E; int dist[V]; // Step 1: Initialize distances from src to all other vertices as INFINITE for (int i = 0; i < V; i++) dist[i] = INT_MAX; dist[src] = 0; // Step 2: Relax all edges |V| - 1 times. A simple shortest path from src // to any other vertex can have at-most |V| - 1 edges for (int i = 1; i <= V-1; i++) { for (int j = 0; j < E; j++) { int u = graph->edge[j].src; int v = graph->edge[j].dest; int weight = graph->edge[j].weight; if (dist[u] + weight < dist[v]) dist[v] = dist[u] + weight; } } // Step 3: check for negative-weight cycles. The above step guarantees // shortest distances if graph doesn't contain negative weight cycle. // If we get a shorter path, then there is a cycle. for (int i = 0; i < E; i++) { int u = graph->edge[i].src; int v = graph->edge[i].dest; int weight = graph->edge[i].weight; if (dist[u] + weight < dist[v]) printf("Graph contains negative weight cycle"); } printArr(dist, V); return; } // Driver program to test above functions int main() { int V = 5; // Number of vertices in graph int E = 8; // Number of edges in graph struct Graph* graph = createGraph(V, E); // add edge 0-1 graph->edge[0].src = 0; graph->edge[0].dest = 1; graph->edge[0].weight = -1; // add edge 0-2 graph->edge[1].src = 0; graph->edge[1].dest = 2; graph->edge[1].weight = 4; // add edge 1-2 graph->edge[2].src = 1; graph->edge[2].dest = 2; graph->edge[2].weight = 3; // add edge 1-3 graph->edge[3].src = 1; graph->edge[3].dest = 3; graph->edge[3].weight = 2; // add edge 1-4 graph->edge[4].src = 1; graph->edge[4].dest = 4; graph->edge[4].weight = 2; // add edge 3-2 graph->edge[5].src = 3; graph->edge[5].dest = 2; graph->edge[5].weight = 5; // add edge 3-1 graph->edge[6].src = 3; graph->edge[6].dest = 1; graph->edge[6].weight = 1; // add edge 4-3 graph->edge[7].src = 4; graph->edge[7].dest = 3; graph->edge[7].weight = -3; BellmanFord(graph, 0); return 0; }