sciion 9ed274dc4e Update README.md | 3 years ago | |
---|---|---|
.settings | 3 years ago | |
bin | 3 years ago | |
gradle/wrapper | 3 years ago | |
src | 3 years ago | |
web | 3 years ago | |
.classpath | 3 years ago | |
.gitattributes | 4 years ago | |
.gitignore | 3 years ago | |
.project | 3 years ago | |
.travis.yml | 3 years ago | |
README.md | 3 years ago | |
build.gradle | 3 years ago | |
gradlew | 3 years ago | |
gradlew.bat | 3 years ago | |
settings.gradle | 3 years ago |
Graphs in a .jar
The library consists of three main parts
Creating a graph with Integer
as Nodes and Double
for Edges and adding some nodes and edges.
Graph<Integer,Double> graph = new Graph<Integer, Double>();
graph.addNode(1);
graph.addNode(2);
graph.addNode(3);
graph.addNode(4);
graph.addNode(5);
//Cost of the edges is defaulted to null
graph.addEdge(1,2);
graph.addEdge(1,3);
graph.addEdge(1,5);
graph.addEdge(2,4);
graph.addEdge(3,4);
Creating a graph and setting all edge costs to the sum of the nodes
Graph<Integer,Integer> graph = new Graph<Integer, Integer>();
graph.addNode(1);
graph.addNode(2);
graph.addNode(3);
graph.addNode(10);
//Cost of the edges is defaulted to null
graph.addEdge(1,2);
graph.addEdge(1,3);
graph.addEdge(3,2);
graph.addEdge(2,10);
graph.forEachEdge((a,b) -> a+b);
Many scenarios involves topologies with a fixed set of states but with dynamic edges. This can be represented using the PropagationGraph
class.
What sets the PropagationGraph
apart from the standrad Graph
datastructure is that PropagationGraph
allow for event listeners on modification of the graph structure. Both for nodes and edges.
PropagationGraph<Integer, Integer> graph = new PropagationGraph<Integer, Integer>();
graph.addNode(1);
graph.addNode(2);
graph.addNode(3);
graph.addEdge(1, 2, 0);
graph.addEdge(2, 3, 0);
graph.addEdge(3, 1, 0);
graph.addEdgeListener((a,b,e,c) ->
{
if (c == GraphChanges.CHANGE)
System.out.println("The values of edges " + a +"->" + b + " changed to " + e);
});
graph.setEdgeValue(1,2,10);
The library contains a wrapper class around a Finite State Machine (FSM). The Finite State Machine uses a wrapper class (FSM
) around the Graph data structure to limit the functionality of the Graph data structure to the constraints of a Finite State Machine.
The following code exemplifies how the FSM wrapper class can be used.
FSM<Integer,Integer> fsm = new FSM<Integer,Integer>();
fsm.addState(1);
fsm.addState(2);
fsm.addState(3);
fsm.addTransition(1,2,1);
fsm.addTransition(1,1,0);
fsm.addTransition(2,3,1);
fsm.addTransition(2,2,0);
fsm.addTransition(3,1,1);
fsm.addTransition(3,3,0);
fsm.setCurrentState(1);
To run the FSM you only need to present an input-sequence and a loop.
String sequence = "0110001111";
for(int i = 0; i < sequence.length(); i++)
fsm.register(sequence.charAt(i) - '0');
The following image represents the FSM from the example above.
The library also contains a wrapper class around the model of a Markov chain.
Example of how the MarkovChain
wrapper class can be used
MarkovChain<String> mc = new MarkovChain<String>();
mc.addState("Sunny");
mc.addState("Rainy");
mc.addTransition("Sunny","Rainy",0.1);
mc.addTransition("Sunny","Sunny",0.9);
mc.addTransition("Rainy","Sunny",0.5);
mc.addTransition("Rainy","Rainy",0.5);
mc.setCurrentState("Sunny");
for(int i = 0; i <100; i++){
mc.register();
System.out.println(mc.getCurrentState());
}
The example will produce the following Markov chain and print the result of 100 iterations of the model.
An example would be if you want to use the GraphUtils
for searching in a state graph like n-puzzle solving, you would need to implement everything in AbstractGraph
even thought A* only needs a Comparator<State>
and the .getNeighbors()
.
What the example looks like using Hamming Distance.
Javadocs can be found in the repository