consolidate all repos to one for archive
This commit is contained in:
3
semester_2/osnove_algoritmov/naloga_4/.gitignore
vendored
Normal file
3
semester_2/osnove_algoritmov/naloga_4/.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
.idea/*
|
||||
cmake-build-debug/*
|
||||
CMakeLists.txt
|
1
semester_2/osnove_algoritmov/naloga_4/README.md
Normal file
1
semester_2/osnove_algoritmov/naloga_4/README.md
Normal file
@@ -0,0 +1 @@
|
||||
Navodila v folder navodila
|
145
semester_2/osnove_algoritmov/naloga_4/main.cpp
Normal file
145
semester_2/osnove_algoritmov/naloga_4/main.cpp
Normal file
@@ -0,0 +1,145 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
struct vozlisce {
|
||||
int vez, predhodnik, cena;
|
||||
};
|
||||
|
||||
int izlociNajmanjsega(std::vector<int> &voz, const std::vector<vozlisce> &rez) {
|
||||
int tmp = voz[0];
|
||||
int rem_voz = 0;
|
||||
for (int i = 0; i < voz.size(); ++i) {
|
||||
if (rez[tmp].cena > rez[voz[i]].cena) {
|
||||
tmp = rez[voz[i]].vez;
|
||||
rem_voz = i;
|
||||
}
|
||||
}
|
||||
|
||||
if(rem_voz == 0){
|
||||
voz.erase(voz.begin());
|
||||
}else {
|
||||
voz.erase(voz.begin() + rem_voz);
|
||||
}
|
||||
return tmp;
|
||||
}
|
||||
|
||||
void dijkstrovAlgoritem(int **&G, int s, const int st_vozlisc, std::vector<vozlisce> &rez) {
|
||||
std::vector<int> Q;
|
||||
for (int i = 0; i < st_vozlisc; ++i) {
|
||||
vozlisce tmp;
|
||||
tmp.vez = i;
|
||||
tmp.predhodnik = -1;
|
||||
tmp.cena = INT32_MAX;
|
||||
Q.push_back(i);
|
||||
rez.push_back(tmp);
|
||||
}
|
||||
|
||||
rez[s].cena = 0;
|
||||
int preg_voz = 0;
|
||||
while (!Q.empty()) {
|
||||
preg_voz = izlociNajmanjsega(Q, rez);
|
||||
for (int i = 0; i < st_vozlisc; i++) {
|
||||
if (G[preg_voz][i] > 0) {
|
||||
if(rez[i].cena > rez[preg_voz].cena + G[preg_voz][i]){
|
||||
rez[i].cena = rez[preg_voz].cena + G[preg_voz][i];
|
||||
rez[i].predhodnik = preg_voz;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void izpisPoti(const std::vector<vozlisce> &rez, const int s, int v){
|
||||
if(s == rez[v].vez){
|
||||
std::cout << rez[v].vez << " cena: " << rez[v].cena << "\n";
|
||||
return;
|
||||
}else{
|
||||
izpisPoti(rez,s,rez[v].predhodnik);
|
||||
std::cout << rez[v].vez << " cena: " << rez[v].cena << "\n";
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void beri(int **&G, int &st_vozlisc) {
|
||||
int st_povezav;
|
||||
std::ifstream f("graf.txt");
|
||||
f >> st_vozlisc;
|
||||
f >> st_povezav;
|
||||
int v1, v2, cena;
|
||||
|
||||
G = new int *[st_vozlisc];
|
||||
|
||||
for (int i = 0; i < st_vozlisc; i++) {
|
||||
G[i] = new int[st_vozlisc];
|
||||
}
|
||||
|
||||
for (int i = 0; i < st_vozlisc; i++) {
|
||||
for (int j = 0; j < st_vozlisc; j++) {
|
||||
G[i][j] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < st_povezav; i++) {
|
||||
f >> v1 >> v2 >> cena;
|
||||
G[v1 - 1][v2 - 1] = cena;
|
||||
G[v2 - 1][v1 - 1] = cena;
|
||||
}
|
||||
std::cout << "Vozlisca: " << st_vozlisc << " Povezave: " << st_povezav << "\n";
|
||||
|
||||
// for (int i = 0; i < st_vozlisc; ++i) {
|
||||
// for (int j = 0; j < st_vozlisc; ++j) {
|
||||
// std::cout << G[j][i] << " ";
|
||||
// }
|
||||
// std::cout << "\n";
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
void menu(){
|
||||
std::cout << "Dijkstrov algoritem - izbira:\n"
|
||||
"1 Nalozi graf\n"
|
||||
"2 Zagon algoritma\n"
|
||||
"3 Izpis najkrajse poti\n"
|
||||
"0 Konec\n\n"
|
||||
"Vasa izbira: ";
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::cout << "Hello, World!" << std::endl;
|
||||
int **G;
|
||||
int st_vozlisc = 0;
|
||||
std::vector<vozlisce> rez;
|
||||
|
||||
bool runing = true;
|
||||
int condition;
|
||||
int tmp, iskanoVozlisce;
|
||||
while(runing){
|
||||
menu();
|
||||
std::cin >> condition;
|
||||
switch (condition) {
|
||||
case 1:
|
||||
beri(G, st_vozlisc);
|
||||
break;
|
||||
case 2:
|
||||
std::cout << "Iskano vozlisce: ";
|
||||
std::cin >> iskanoVozlisce;
|
||||
dijkstrovAlgoritem(G, iskanoVozlisce, st_vozlisc, rez);
|
||||
break;
|
||||
case 3:
|
||||
// for (int i = 0; i < rez.size(); ++i) {
|
||||
// std::cout << rez[i].vez << " " << rez[i].predhodnik << " " << rez[i].cena << "\n";
|
||||
// }
|
||||
std::cout << "Izberi vozlisce: ";
|
||||
std::cin >> tmp;
|
||||
izpisPoti(rez,iskanoVozlisce, tmp);
|
||||
break;
|
||||
case 0:
|
||||
runing = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
BIN
semester_2/osnove_algoritmov/naloga_4/navodila/Vaja_4.pdf
Normal file
BIN
semester_2/osnove_algoritmov/naloga_4/navodila/Vaja_4.pdf
Normal file
Binary file not shown.
14
semester_2/osnove_algoritmov/naloga_4/navodila/graf.txt
Normal file
14
semester_2/osnove_algoritmov/naloga_4/navodila/graf.txt
Normal file
@@ -0,0 +1,14 @@
|
||||
8
|
||||
12
|
||||
1 2 3
|
||||
1 3 6
|
||||
1 8 1
|
||||
2 3 2
|
||||
2 8 2
|
||||
3 4 1
|
||||
4 5 6
|
||||
4 6 3
|
||||
4 7 4
|
||||
5 6 3
|
||||
6 7 2
|
||||
7 8 10
|
Reference in New Issue
Block a user