Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
20 changes: 20 additions & 0 deletions Reseno/PretrageOsnovno.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PretrageOsnovno", "PretrageOsnovno\PretrageOsnovno.csproj", "{C78D10E9-7D95-40AA-B84A-2931B8DDC038}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x86 = Debug|x86
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C78D10E9-7D95-40AA-B84A-2931B8DDC038}.Debug|x86.ActiveCfg = Debug|x86
{C78D10E9-7D95-40AA-B84A-2931B8DDC038}.Debug|x86.Build.0 = Debug|x86
{C78D10E9-7D95-40AA-B84A-2931B8DDC038}.Release|x86.ActiveCfg = Release|x86
{C78D10E9-7D95-40AA-B84A-2931B8DDC038}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
Binary file added Reseno/PretrageOsnovno.suo
Binary file not shown.
77 changes: 77 additions & 0 deletions Reseno/PretrageOsnovno/AStarSearch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PretrageOsnovno
{
class AStarSearch
{

public State Search(string startNodeName, string endNodeName,bool dodatni)
{
Node startNode = Program.instance.graph[startNodeName];
Node endNode = Program.instance.graph[endNodeName];

List<State> stanjaZaObradu = new List<State>();

stanjaZaObradu.Add(new State(startNode));

while (stanjaZaObradu.Count > 0)
{
State naObradi = stanjaZaObradu[0];
stanjaZaObradu.Remove(naObradi);

if (naObradi.Node.Name == endNode.Name)
{
return naObradi;
}
else
{
List<State> mogucaSledecaStanja = naObradi.children();
stanjaZaObradu.InsertRange(0, mogucaSledecaStanja);
sortiraj(stanjaZaObradu,endNode,dodatni);
}
}
return null;
}


// metoda koja sortira listu u rastucem redosledu zbira cene i heuristike
private void sortiraj(List<State> lista, Node endNode,bool dodatni)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sortiranje je "skupa" operacija. Teorijski je tacno ovo sto si ti uradio, ali je sa stanovista implementacije bolje pronaci minimalni element i njega poslati na obradu.

{

if(lista.Count>0){
//ukoliko ima sta da sortira
//bubble sort
bool flag = false;
for (int i = 1; (i <= (lista.Count - 1))&&flag; i++)
{
for (int j = 0; j < (lista.Count - 1); j++)
{
if (dodatni)
{//ulazi u slucaju ako je dodatni zadatak
((Grad)lista[j + 1].Node).izracunajHeuristic(((Grad)endNode).Position);
((Grad)lista[j].Node).izracunajHeuristic(((Grad)endNode).Position);
}

double temp1 = lista[j + 1].Node.Heuristic + lista[j + 1].Cost;
double temp2 = lista[j].Node.Heuristic + lista[j].Cost;
if (temp1 > temp2)
{
var temp = lista[j];
lista[j] = lista[j + 1];
lista[j + 1] = temp;
flag = true;
}
}


}

}

}

}
}
39 changes: 39 additions & 0 deletions Reseno/PretrageOsnovno/BreadthFirstSearch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PretrageOsnovno
{
class BreadthFirstSearch
{
public State Search(string startNodeName, string endNodeName)
{
Node startNode = Program.instance.graph[startNodeName];
Node endNode = Program.instance.graph[endNodeName];

List<State> stanjaZaObradu = new List<State>();

stanjaZaObradu.Add(new State(startNode));

while (stanjaZaObradu.Count > 0)
{
State naObradi = stanjaZaObradu[0];
stanjaZaObradu.Remove(naObradi);

if (naObradi.Node.Name == endNode.Name)
{
return naObradi;
}
else
{
List<State> mogucaSledecaStanja = naObradi.children();
stanjaZaObradu.AddRange(mogucaSledecaStanja);
}
}
return null;
}
}
}
39 changes: 39 additions & 0 deletions Reseno/PretrageOsnovno/DepthFirstSearch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace PretrageOsnovno
{
class DepthFirstSearch
{
public State Search(string startNodeName, string endNodeName)
{
Node startNode = Program.instance.graph[startNodeName];
Node endNode = Program.instance.graph[endNodeName];

List<State> stanjaZaObradu = new List<State>();

stanjaZaObradu.Add(new State(startNode));

while (stanjaZaObradu.Count > 0)
{
State naObradi = stanjaZaObradu[0];
stanjaZaObradu.Remove(naObradi);

if (naObradi.Node.Name == endNode.Name)
{
return naObradi;
}
else
{
List<State> mogucaSledecaStanja = naObradi.children();
stanjaZaObradu.InsertRange(0, mogucaSledecaStanja);
}
}
return null;
}
}
}
33 changes: 33 additions & 0 deletions Reseno/PretrageOsnovno/Grad.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Text;

namespace PretrageOsnovno
{
class Grad : Node
{

public Grad(string name, Point position):base(name,0) //cena je u pocetku 0 zato sto nzm koji je ciljni grad
{
this.Position = position;
}

public Point Position { get; set; }
/// <summary>
/// Na osnovu pozicije grada do kog trebamo stici, racuna heuristiku
/// </summary>
/// <param name="p">Pozicija ciljnog grada</param>
public void izracunajHeuristic(Point p)
{
var x1 = Position.X;
var y1 = Position.Y;
var x2 = p.X;
var y2 = p.Y;
Console.WriteLine(" " + Math.Pow(5, 2));
base.Heuristic = (Math.Pow(x1 - x2, 2) + Math.Pow(y1 - y2, 2));

}
}
}
85 changes: 85 additions & 0 deletions Reseno/PretrageOsnovno/Graph.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Threading.Tasks;

namespace PretrageOsnovno
{
class Graph
{
public Dictionary<string, Node> graph = null;

public Graph(string[] linesNodes, string[] linesLinks, bool dodatni)
{
graph = new Dictionary<string, Node>();
formGraph(linesNodes, linesLinks,dodatni);
}


private void formGraph(string[] linesNodes, string[] linesLinks,bool dodatni)
{
if (!dodatni)
{//ukoliko nije dodatni
char[] sep = { ':', ',' };
foreach (String line in linesNodes)
{
string[] lines = line.Split(sep);
Node node = new Node(lines[0], Double.Parse(lines[1]));
graph.Add(lines[0], node);

}
}
else
{
char[] sep = { ':', ',', ')', '(' };
foreach (String line in linesNodes)
{
string[] lines = line.Split(sep);
Point p = new Point(Double.Parse(lines[1]), //x i y kordinata
Double.Parse(lines[2]));

Grad grad = new Grad(lines[0], //naziv
p);//pozicija grada

graph.Add(lines[0], grad);

}

}

char[] separator = { ':', ',' };
foreach (String line in linesLinks)
{
string[] lines = line.Split(separator);
if (graph.ContainsKey(lines[1]))
{

Link link = new Link(graph[lines[1]], //startNode
graph[lines[2]], //endNode
lines[0], //name
Double.Parse(lines[3])); //cost

graph[lines[1]].Links.Add(link);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}

}

#region ispis
public void printGraph()
{
StringBuilder sb = new StringBuilder();
foreach (KeyValuePair<string, Node> kvp in graph)
{
foreach (Link link in kvp.Value.Links)
{
Console.WriteLine(link.Name + ":" + link.StartNode + "," + link.EndNode + "," + link.Cost);
}
}
}
#endregion
}
}
8 changes: 8 additions & 0 deletions Reseno/PretrageOsnovno/Graphs/Graph.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
put:NS,BG,90
put:NS,SB,70
put:SB,UZ,80
put:BG,UZ,90
put:BG,KR,110
put:UZ,KR,40
put:UZ,NI,190
put:KR,NI,120
6 changes: 6 additions & 0 deletions Reseno/PretrageOsnovno/Graphs/Nodes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
NS:250
BG:190
SB:210
UZ:110
KR:100
NI:0
6 changes: 6 additions & 0 deletions Reseno/PretrageOsnovno/Graphs/NodesDodatno.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
NS(20,15)
BG(55,60)
SB(5,40)
UZ(0,120)
KR(30,150)
NI(55,250)
1 change: 1 addition & 0 deletions Reseno/PretrageOsnovno/Graphs/Resenje.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NS,BG,UZ,KR,NI
55 changes: 55 additions & 0 deletions Reseno/PretrageOsnovno/IterativeDepthFirstSearch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace PretrageOsnovno
{
class IterativeDepthFirstSearch
{
private const int MaxLevel = 10000;
private int currentLevel;


public State Search(string startNodeName, string endNodeName)
{

Node startNode = Program.instance.graph[startNodeName];
Node endNode = Program.instance.graph[endNodeName];

List<State> stanjaZaObradu = new List<State>();

stanjaZaObradu.Add(new State(startNode));
currentLevel = 1;
while (stanjaZaObradu.Count > 0 && currentLevel < MaxLevel)
{
State naObradi = stanjaZaObradu[0];
stanjaZaObradu.Remove(naObradi);

if (naObradi.Node.Name == endNode.Name)
{
return naObradi;
}
else
{
if (naObradi.Level < currentLevel)
{
List<State> mogucaSledecaStanja = naObradi.children();
stanjaZaObradu.InsertRange(0, mogucaSledecaStanja);
}
else
{
if (stanjaZaObradu.Count == 0)
{
currentLevel++;
stanjaZaObradu.Add(new State(startNode));
}
}
}
}
return null;
}
}
}
Loading