Skip to content

Instantly share code, notes, and snippets.

@jakewilson801
Created May 10, 2013 03:19
Show Gist options
  • Save jakewilson801/5552192 to your computer and use it in GitHub Desktop.
Save jakewilson801/5552192 to your computer and use it in GitHub Desktop.
Some awesome graph code from school
//
// main.cpp
// Graph
//
// Created by Jacob Wilson on 4/11/12.
// Copyright (c) 2012 Weber State. All rights reserved.
//
#include <iostream>
#include <iomanip>
using namespace std;
const int gSize = 20;
const int infinity = 1000;
class Graph{
public:
void shortestPath(int);
void printShortestDistance(int);
int weights[gSize][gSize];
int smallestw[gSize];
int shortPath[gSize][gSize];
Graph();
};
Graph::Graph()
{
for(int i=0;i<gSize;i++)
{
for(int j=0;j<gSize;j++){
weights[i][j] = infinity;
shortPath[i][j] = infinity;
}
}
weights[0][1] = 43;
weights[0][10] = 26;
weights[0][15] = 34;
weights[1][2] = 8;
weights[1][16] = 14;
weights[1][19] = 18;
weights[1][7] = 15;
weights[1][0] = 43;
weights[2][3] = 16;
weights[2][16] = 26;
weights[3][2] = 16;
weights[3][4] = 50;
weights[3][10] = 33;
weights[4][3] = 50;
weights[4][10] = 27;
weights[4][19] = 6;
weights[5][13] = 44;
weights[5][14] = 23;
weights[5][17] = 5;
weights[6][15] = 2;
weights[6][18] = 16;
weights[7][1] = 15;
weights[7][18] = 19;
weights[10][0] = 26;
weights[10][3] = 33;
weights[10][4] = 27;
weights[10][11] = 25;
weights[11][10] = 48;
weights[11][12] = 3;
weights[11][14] = 7;
weights[11][17] = 20;
weights[12][11] = 3;
weights[12][13] = 20;
weights[13][5] = 44;
weights[13][19] = 28;
weights[13][12] = 20;
weights[14][11] = 28;
weights[14][5] = 23;
weights[15][0] = 34;
weights[15][6] = 41;
weights[15][7] = 54;
weights[16][1] = 15;
weights[16][2] = 26;
weights[17][5] = 5;
weights[17][11] = 20;
weights[17][18] = 16;
weights[18][6] = 16;
weights[18][7] = 19;
weights[18][17] = 16;
weights[19][1] = 18;
weights[19][4] = 6;
weights[19][13] = 28;
}
void Graph::shortestPath(int vertex)
{
for(int i=0;i<gSize;i++){
smallestw[i] = weights[vertex][i];
if(smallestw[i] != infinity){
shortPath[i][0] = vertex;
}
}
bool weightFound[gSize];
for(int i=0;i<gSize;i++)
weightFound[i] = false;
weightFound[vertex] = true;
smallestw[vertex] = 0;
shortPath[vertex][0] = vertex;
for(int i=0;i<gSize-1;i++)
{
int minWeight = 1000;
int v;
for(int j=0;j<gSize;j++)
if(!weightFound[j])
if(smallestw[j] < minWeight)
{
v=j;
minWeight = smallestw[v];
}
weightFound[v] = true;
for(int j=0;j<gSize;j++){
if(!weightFound[j]){
if(minWeight + weights[v][j] < smallestw[j]){
smallestw[j] = minWeight + weights[v][j];
bool foundInf = false;
for(int x = 0; x < gSize; x++){
if((shortPath[v][x] == infinity) && (!foundInf)){
foundInf = true;
shortPath[j][x] = v;
}else
shortPath[j][x] = shortPath[v][x];
}
}
}
}
}
}
void Graph::printShortestDistance(int vertex)
{
cout << "Source Vertex: " << vertex << endl;
cout << "Shortest distance from source to each vertex." << endl;
cout << "Vertex * Shortest_Distance * Shortest_Path" << endl;
for(int j=0;j<gSize;j++){
if(smallestw[j] != infinity)
cout << setw(4) << j << setw(12) << smallestw[j] <<setw(15);
else
cout << setw(4) << j << setw(12) << "Isn't there" << setw(25);
for(int x = 0; x < gSize; x++){
if(shortPath[j][x]!= infinity){
cout << shortPath[j][x] << " " ;
}
}
cout << endl;
}
}
int main(){
Graph theGraph;
int vertex;
cout << "Enter a vertex ";
cin >> vertex;
theGraph.shortestPath(vertex);
theGraph.printShortestDistance(vertex);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment