mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-04-15 03:10:07 +08:00
formatting source-code for 153fb7b8a5
This commit is contained in:
@@ -6,8 +6,7 @@ using namespace std;
|
||||
|
||||
int graph[V][V] = {{0, 5, 1, 2}, {5, 0, 3, 3}, {1, 3, 0, 4}, {2, 3, 4, 0}};
|
||||
|
||||
struct mst
|
||||
{
|
||||
struct mst {
|
||||
bool visited;
|
||||
int key;
|
||||
int near;
|
||||
@@ -15,10 +14,8 @@ struct mst
|
||||
|
||||
mst MST_Array[V];
|
||||
|
||||
void initilize()
|
||||
{
|
||||
for (int i = 0; i < V; i++)
|
||||
{
|
||||
void initilize() {
|
||||
for (int i = 0; i < V; i++) {
|
||||
MST_Array[i].visited = false;
|
||||
MST_Array[i].key = INFINITY; // considering INFINITY as inifinity
|
||||
MST_Array[i].near = i;
|
||||
@@ -27,17 +24,13 @@ void initilize()
|
||||
MST_Array[0].key = 0;
|
||||
}
|
||||
|
||||
void updateNear()
|
||||
{
|
||||
for (int v = 0; v < V; v++)
|
||||
{
|
||||
void updateNear() {
|
||||
for (int v = 0; v < V; v++) {
|
||||
int min = INFINITY;
|
||||
int minIndex = 0;
|
||||
for (int i = 0; i < V; i++)
|
||||
{
|
||||
for (int i = 0; i < V; i++) {
|
||||
if (MST_Array[i].key < min && MST_Array[i].visited == false &&
|
||||
MST_Array[i].key != INFINITY)
|
||||
{
|
||||
MST_Array[i].key != INFINITY) {
|
||||
min = MST_Array[i].key;
|
||||
minIndex = i;
|
||||
}
|
||||
@@ -45,12 +38,9 @@ void updateNear()
|
||||
|
||||
MST_Array[minIndex].visited = true;
|
||||
|
||||
for (int i = 0; i < V; i++)
|
||||
{
|
||||
if (graph[minIndex][i] != 0 && graph[minIndex][i] < INFINITY)
|
||||
{
|
||||
if (graph[minIndex][i] < MST_Array[i].key)
|
||||
{
|
||||
for (int i = 0; i < V; i++) {
|
||||
if (graph[minIndex][i] != 0 && graph[minIndex][i] < INFINITY) {
|
||||
if (graph[minIndex][i] < MST_Array[i].key) {
|
||||
MST_Array[i].key = graph[minIndex][i];
|
||||
MST_Array[i].near = minIndex;
|
||||
}
|
||||
@@ -59,17 +49,14 @@ void updateNear()
|
||||
}
|
||||
}
|
||||
|
||||
void show()
|
||||
{
|
||||
for (int i = 0; i < V; i++)
|
||||
{
|
||||
void show() {
|
||||
for (int i = 0; i < V; i++) {
|
||||
cout << i << " - " << MST_Array[i].near << "\t"
|
||||
<< graph[i][MST_Array[i].near] << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
initilize();
|
||||
updateNear();
|
||||
show();
|
||||
|
||||
Reference in New Issue
Block a user