Professional Documents
Culture Documents
//
//
//
//
//
//
main.m
BellmanFord
Created by Kyler Edwards on 7/16/14.
Copyright (c) 2014 Kyler. All rights reserved.
#import
#import
#import
#import
<Foundation/Foundation.h>
"Vertex.h"
"Edge.h"
"Graph.h"
alloc]
alloc]
alloc]
alloc]
alloc]
alloc]
alloc]
init];
init];
init];
init];
init];
init];
init];
//Create Graph and add the vertices and edges into the graph
Graph* graph = [[Graph alloc] init];
[graph.listOfVertices
[graph.listOfVertices
[graph.listOfVertices
[graph.listOfVertices
addObject:v1];
addObject:v2];
addObject:v3];
addObject:v4];
[graph.listOfVertices addObject:v5];
[graph.listOfVertices addObject:v6];
[graph.listOfVertices addObject:v7];
[graph.listOfEdges
[graph.listOfEdges
[graph.listOfEdges
[graph.listOfEdges
[graph.listOfEdges
[graph.listOfEdges
[graph.listOfEdges
[graph.listOfEdges
[graph.listOfEdges
[graph.listOfEdges
addObject:e1];
addObject:e2];
addObject:e3];
addObject:e4];
addObject:e5];
addObject:e6];
addObject:e7];
addObject:e8];
addObject:e9];
addObject:e10];
NSLog(@"*********");
if (isThereASolution == YES) {
NSLog(@"YES");
} else {
NSLog(@"NO");
}
for (Vertex* V in anotherGraph.listOfVertices) {
NSLog(@"%f", V.key);
}
}// end of autoreleasepool
return 0;
}
static BOOL bellmanFord(Graph* G, Vertex* S)
{
//INIT
for (Vertex* V in G.listOfVertices) {
V.key = INFINITY;
V.parent = NULL;
}
S.key = 0;
//RELAX EDGES
for (int i = 1; i <= G.listOfVertices.count - 1; i++) {
for (Edge* E in G.listOfEdges) {
if (E.destination.key > E.source.key + E.weight) {
E.destination.key = E.source.key + E.weight;
E.destination.parent = E.source;
}
}
}
//CHECK FOR NEGATIVE-WEIGHT CYCLES
for (Edge* E in G.listOfEdges) {
if (E.destination.key > E.source.key + E.weight) {
return NO;
}
}
return YES;
}