You are on page 1of 3

//

//
//
//
//
//
//

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"

static BOOL bellmanFord(Graph* G, Vertex* S);


int main(int argc, const char * argv[])
{
//I changed the class Edge and Vertex's properties to different names
//You will need to change them back to fit this project
@autoreleasepool {
//Create Vertices
Vertex* v1 = [[Vertex
Vertex* v2 = [[Vertex
Vertex* v3 = [[Vertex
Vertex* v4 = [[Vertex
Vertex* v5 = [[Vertex
Vertex* v6 = [[Vertex
Vertex* v7 = [[Vertex
//Create Edges
Edge* e1 = [[Edge
e1.weight = 3;
Edge* e2 = [[Edge
e2.weight = 6;
Edge* e3 = [[Edge
e3.weight = 2;
Edge* e4 = [[Edge
e4.weight = 6;
Edge* e5 = [[Edge
e5.weight = 4;
Edge* e6 = [[Edge
e6.weight = 11;
Edge* e7 = [[Edge
e7.weight = 9;
Edge* e8 = [[Edge
e8.weight = 7;
Edge* e9 = [[Edge
e9.weight = 8;
Edge* e10 = [[Edge
e10.weight = 5;

alloc]
alloc]
alloc]
alloc]
alloc]
alloc]
alloc]

init];
init];
init];
init];
init];
init];
init];

alloc] init]; e1.source = v1; e1.destination = v2;


alloc] init]; e2.source = v1; e2.destination = v3;
alloc] init]; e3.source = v2; e3.destination = v3;
alloc] init]; e4.source = v3; e4.destination = v5;
alloc] init]; e5.source = v5; e5.destination = v7;
alloc] init]; e6.source = v2; e6.destination = v7;
alloc] init]; e7.source = v2; e7.destination = v6;
alloc] init]; e8.source = v6; e8.destination = v7;
alloc] init]; e9.source = v4; e9.destination = v6;
alloc] init]; e10.source = v2; e10.destination = v4;

//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];

//Run the bellmanFord algorithm on the graph


BOOL isThereASolution = bellmanFord(graph, v1);
//Display the result and the list of vertices with their distances/value
s
NSLog(@"*********");
if (isThereASolution == YES) {
NSLog(@"YES");
} else {
NSLog(@"NO");
}
for (Vertex* V in graph.listOfVertices) {
NSLog(@"%f", V.key);
}
//Create vertices for anotherGraph
Vertex* vert1 = [[Vertex alloc] init];
Vertex* vert2 = [[Vertex alloc] init];
Vertex* vert3 = [[Vertex alloc] init];
//Create edges for anotherGraph
Edge* edge1 = [[Edge alloc] init]; edge1.source = vert1; edge1.destinati
on = vert2; edge1.weight = -2;
Edge* edge2 = [[Edge alloc] init]; edge2.source = vert2; edge2.destinati
on = vert3; edge2.weight = 1;
Edge* edge3 = [[Edge alloc] init]; edge3.source = vert3; edge3.destinati
on = vert1; edge3.weight = -1;
//Create anotherGraph and add the new vertices and edges to it
Graph* anotherGraph = [[Graph alloc] init];
[anotherGraph.listOfVertices addObject:vert1];
[anotherGraph.listOfVertices addObject:vert2];
[anotherGraph.listOfVertices addObject:vert3];
[anotherGraph.listOfEdges addObject:edge1];
[anotherGraph.listOfEdges addObject:edge2];
[anotherGraph.listOfEdges addObject:edge3];
//Run the bellmanFord algorithm on anotherGraph
isThereASolution = bellmanFord(anotherGraph, vert1);
//Display the result and the list of vertices with their distances/value
s

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;
}

You might also like