![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 346
Rep Power: 4
![]() |
yea it is two nodes the row is one node and the column is the other node and they are connected but the connection has a weight to it which is the value at [row][column]
|
|
|
|
|
|
#12 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
So an entire row is one node that can connect to other nodes represented by columns, and another row is just another node that can connect to other nodes represented by columns, and may in fact have a connection to a column node, said column node being shared with any number of other row-nodes, each pair having its own weight. Is that right? If so, I'll read the main problem again.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#13 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 346
Rep Power: 4
![]() |
i believe that is right i figured it out though, sorry for all the trouble. I will post the final code if you want to look at it.
//matrix implementation of prim's algorithm
#include <iostream>
#include<math.h>
#import<string.h>
using namespace std;
int matrix[50][50];
int finishedWeight[50];
int finishedVertices[50];
char edges[50][20];
int counter = 0;
int size = 0;
//fill the matrix with a big number
void fillMatrix(int vertexNum)
{
for(int i = 0; i < vertexNum; i++)
{
for(int j = 0; j < vertexNum; j++)
{
matrix[i][j] = pow(2,32) - 1;
}
}
}
//set the weight of the vertices
void insert(int start, int end, int weight)
{
matrix[start][end] = weight;
}
//add the edge for an undirected graph
void addUndirected(int vertexNum)
{
int start = 0;
int end = 0;
int weight;
size = vertexNum;
fillMatrix(vertexNum);
while(start != -1)
{
cout << "Please enter a starting vertex: ";
cin >> start;
cout << "Please enter an ending vertex: ";
cin >> end;
cout << "Please enter a weight: ";
cin >> weight;
insert(start, end, weight);
insert(end, start, weight);
}
}
// add the edge for a directed graph
void addDirected(int vertexNum)
{
int start = 0;
int end = 0;
int weight;
size = vertexNum;
fillMatrix(vertexNum);
while(start != -1)
{
cout << "Please enter a starting vertex: ";
cin >> start;
cout << "Please enter an ending vertex: ";
cin >> end;
cout << "Please enter a weight: ";
cin >> weight;
insert(start, end, weight);
}
}
// checks to see if the value is in the array
bool isInArray(int ar[], int value, int size)
{
bool check = false;
for(int i = 0; i < size; i++)
{
if(ar[i] == value)
check = true;
}
return check;
}
//execute the algorithm
void prim()
{
int minx;
int miny;
int firstVertex;
int check = 0;
int minWeight = pow(2, 32) - 1;
for(int k = 0; k < counter; k++)
{
firstVertex = finishedVertices[k];
for(int l = 0; l < size; l++)
{
if(matrix[firstVertex][l] < minWeight && !isInArray(finishedVertices, l, counter))
{
minx = firstVertex;
miny = l;
minWeight = matrix[firstVertex][l];
check = 1;
}
}
}
if(check == 1)
{
finishedVertices[counter] = miny;
finishedWeight[counter] = minWeight;
sprintf(edges[counter], "edge %d: %d - %d.\n", counter, minx, miny);
counter++;
prim();
}
}
// print the vertices and the corresponding weight and each edge
void print()
{
for(int i = 0; i < counter; i++)
{
cout << finishedVertices[i] << "\t" << finishedWeight[i] << endl;
}
for(int u = 0; u < counter; u++)
cout << edges[u];
}
//0 out finishedVertices[], finishedWeights[], and counter
void reset()
{
for(int i = 0; i < size; i++)
finishedVertices[i] = 0;
for(int j = 0; j < size; j++)
finishedWeight[j] = 0;
counter = 0;
}
// run functions and prompts
int main()
{
int option = 0; // option for starting vertex for algorithm
int option1 = 0; // option for directed or undirected
int option2; // option to see if the algorithm is run again
int vertexNum = 0;
while(option1 != 3)
{
option2 = 1;
cout << "1. Directed Graph. \n"
<< "2. Undirected Graph. \n"
<< "3. Exit. \n"
<< "Selection: ";
cin >> option1;
if(option1 == 1)
{
cout << "Enter the number of vertices ";
cin >> vertexNum;
addDirected(vertexNum);
while(option2 == 1)
{
reset();
cout << "Please enter a starting vertex for the algorithm ";
cin >> option;
finishedVertices[0] = option;
counter++;
prim();
print();
cout << "Press 1 to run the algorithm with a different starting vertex or 2 to go back to the main menu ";
cin >> option2;
}
}
else if(option1 == 2)
{
cout << "Enter the number of vertices ";
cin >> vertexNum;
addUndirected(vertexNum);
while(option2 == 1)
{
reset();
cout << "Please enter a starting vertex for the algorithm ";
cin >> option;
finishedVertices[0] = option;
counter++;
prim();
print();
//display();
cout << "Press 1 to run the algorithm with a different starting vertex or 2 to go back to the main menu ";
cin >> option2;
}
}
}
return 0;
} |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|