Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruno Sacaric committed May 13, 2019
0 parents commit 2ab9e64
Show file tree
Hide file tree
Showing 16 changed files with 233 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/description.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions Zavrsni_DFSiBFS.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

2 changes: 2 additions & 0 deletions src/A_Star.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
public abstract class A_Star extends GraphSearchAlgorithm{
}
18 changes: 18 additions & 0 deletions src/BFS.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import java.util.*;

public class BFS extends GraphSearchAlgorithm{

@Override
AbstractCollection<Node> initOpen() {
return new LinkedList<>();
}

@Override
Node nextNode(AbstractCollection open) throws Exception {
if(open instanceof Queue) {
return ((Queue<Node>)open).remove();
} else{
throw new Exception("Krivog tipa je open");
}
}
}
19 changes: 19 additions & 0 deletions src/DFS.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import java.util.AbstractCollection;
import java.util.Stack;

public class DFS extends GraphSearchAlgorithm{

@Override
AbstractCollection<Node> initOpen() {
return new Stack<>();
}

@Override
Node nextNode(AbstractCollection open) throws Exception {
if(open instanceof Stack) {
return ((Stack<Node>)open).pop();
} else{
throw new Exception("Krivog tipa je open");
}
}
}
32 changes: 32 additions & 0 deletions src/GraphSearchAlgorithm.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import java.util.*;

abstract class GraphSearchAlgorithm {

public List<Node> run(Node s0, SearchProblem problem) throws Exception {
Node node = s0;
AbstractCollection<Node> open = initOpen();
open.add(node);
Set<Node> closed = new HashSet<>();
while(! open.isEmpty()){
node = nextNode(open);
if(problem.isGoalState(node)){
return node.path();
}
if(!closed.contains(node)){
closed.add(node);
Map<Node, Double> succ = problem.successors(node);
for(Node m : succ.keySet()){
//if(m.getCost() == null || m.getCost() > node.getCost() + succ.get(m)){
m.setCost(node.getCost() + succ.get(m));
open.add(m);
}
}
}
return null;
}

abstract AbstractCollection<Node> initOpen();

abstract Node nextNode(AbstractCollection open) throws Exception;

}
7 changes: 7 additions & 0 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
public class Main {

public static void main(String[] args) {
Problem[] problems = Reader.getProblems();

}
}
52 changes: 52 additions & 0 deletions src/Node.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import java.util.LinkedList;
import java.util.List;

class Node {

private String name;
private Node parent;
private boolean isRootNode;
private Double cost;

public Node(String name, Node parent, boolean isRootNode){
this.name = name;
this.parent = parent;
this.isRootNode = isRootNode;
}

List<Node> path() {
List<Node> list;
if(isRootNode) {
list = new LinkedList<>();
} else {
list = parent.path();
}
list.add(0, this);
return list;
}

public boolean isRootNode() {
return isRootNode;
}

public Node getParent() {
return parent;
}

public String getName(){
return name;
}

public void setCost(Double cost){
this.cost = cost;
}

public Double getCost(){
return cost;
}

@Override
public String toString() {
return name;
}
}
21 changes: 21 additions & 0 deletions src/Problem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import java.util.List;
import java.util.Map;

class Problem {

Map<Node, Map<Node, Double>> successors;

Problem(Map<Node, Map<Node, Double>> successors){
this.successors = successors;
}

boolean isGoalState(Node n) {
return n.isRootNode();
}

Map<Node, Double> successors(Node n) {
return successors.get(n);
}
//!!!!pospremaj svako stanje jednom u memoriju za svaki par stanje-roditelj, da se moze koristiti path()
//da vrati put do stanja
}
5 changes: 5 additions & 0 deletions src/Reader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Reader {
static Problem[] getProblems(){
return null;
}
}
9 changes: 9 additions & 0 deletions src/SearchProblem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import java.util.List;
import java.util.Map;

class SearchProblem extends Problem {

SearchProblem(Map<Node, Map<Node, Double>> successors) {
super(successors);
}
}

0 comments on commit 2ab9e64

Please sign in to comment.