NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MELKOR 1000000

/*
Aşağıdaki matrisi kopyalayıp matris.txt dosyasına kaydedin. Bu bizim kullanacağımız veri yapısı olacak
Unutmayın, kodunuzla aynı klasörde olmalı.
0 2 0 6 0
2 0 3 8 5
0 3 0 0 7
6 8 0 0 9
0 5 7 9 0
*/

//Matrisimizi tutacağımzı dizi
int Array[5][5];

//Matrisimizi matris.txt dosyasından okuyup dizimize aktaran fonksiyon
void readMatrix()
{
int i = 0;
FILE *fp = fopen("matris.txt", "r");

while(fscanf(fp, "%d %d %d %d %d",
&Array[i][0],
&Array[i][1],
&Array[i][2],
&Array[i][3],
&Array[i][4]
) != EOF)
{
i = i + 1 ;
}
}

//En kısa yolun indisini geri döndüren yardımcı fonksiyon. Hocalar burayı sorarlar dikkat edin.
int minKey(int key[], bool visited[])
{
int min = MELKOR;
int minIndex, i;
for( i = 0; i < 5; i++)
{
//Eğer bir düğüm ziyaret edilmemiş ve min değerinden daha küçükse burayı seçiyoruz.
if(visited[i] == false && key[i] < min)
{
min = key[i];
minIndex = i;
}
}
//minIndex'i return ediyoruz.
return minIndex;
}

//Esas fonksiyon burada, veri yapımızı tutan diziyi parametre olarak veriyoruz.
void prims(int Array[5][5])
{
//Genel değişkenler
int i, j, t;
int key[5];
int parent[5];
bool visited[5];

//Tüm düğümleri ziyaret edilmemiş olarak işaretleyip, maliyeti sonsuz yapıyoruz.
for(i = 0; i < 5; i++)
{
visited[i] = false;
key[i] = MELKOR;
}

//Başlangıç düğümünü belirliyoruz. Maliyeti sıfır olur başlangıç düğümünün. Parentı olmadığı için -1 veriyoruz.
key[0] = 0;
parent[0] = -1;

//5 düğümümüz olduğu için dış döngümüz 5 defa dönecek
for(j = 0; j < 5; j++)
{
//En kısa yolu u değerine atıyoruz
int u = minKey(key, visited);
//Ziyaret edildi olarak işaretliyoruz.
visited[u] = true;

//Fonksiyonun can damarı
for(t = 0; t < 5; t++)
{
//Asgari tarama ağacına en az maliyete sahip ziyaret edilmemiş düğümü ekliyoruz. Ancak Parent'ı tutmayı unutmuyoruz.
if(Array[u][t] && visited[t] == false && Array[u][t] < key[t])
{
key[t] = Array[u][t];
parent[t] = u;
}
}
}
printMST(parent, Array);

}

//Asgari tarama ağacını yazdıran fonksiyon
int printMST(int parent[], int Array[5][5])
{
int i;
for (i = 1; i < 5; i++)
printf("%d - %d %d n", parent[i], i, Array[i][parent[i]]);
}

int main()
{
readMatrix();
prims(Array);
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.