DesktopLauncher.kt
-----------------------------------
package com.vperahud.desktop
import com.badlogic.gdx.backends.lwjgl.LwjglApplication
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration
import com.vperahud.Main
fun main(args: Array<String>) {
val config = LwjglApplicationConfiguration()
LwjglApplication(Main(),config)
config.title = "Hi Kotlin!"
config.width = 1024
config.height = 768
}
Main.kt
--------------------------
package com.vperahud
import com.badlogic.gdx.ApplicationAdapter
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.graphics.GL20
import com.badlogic.gdx.graphics.Texture
import com.badlogic.gdx.graphics.g2d.SpriteBatch
class Main : ApplicationAdapter() {
lateinit var batch: SpriteBatch
lateinit var img: Texture
override fun create() {
batch= SpriteBatch()
img= Texture("badlogic.jpg")
}
override fun render() {
Gdx.gl.glClearColor(1f, 0f, 0f, 1f)
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT)
batch.begin()
batch.draw(img, 0f, 0f)
batch.end()
}
override fun dispose() {
batch.dispose()
img.dispose()
}
}
понедельник, 7 мая 2018 г.
вторник, 19 декабря 2017 г.
Dijkstra (Graph)

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
public class SearchAlgorithms {
public static <T> void Dijkstra(Graph<T> graph, Vertex<T> source) {
ArrayList<Vertex<T>> unfinishedVertices = new ArrayList<Vertex<T>>();
for (Vertex<T> vertex : graph.Vertices()) {
vertex.Distance = Integer.MAX_VALUE;
vertex.Parent = null;
unfinishedVertices.add(vertex);
}
source.Distance = 0;
while (unfinishedVertices.size() > 0) {
Vertex<T> vertex = GetClosestVertex(unfinishedVertices);
unfinishedVertices.remove(vertex);
for (Vertex<T> adjVertex : graph.GetAdjacentVertices(vertex)) {
if (adjVertex.Distance > vertex.Distance + graph.GetEdgeWeight(vertex, adjVertex)) {
adjVertex.Distance = vertex.Distance + graph.GetEdgeWeight(vertex, adjVertex);
adjVertex.Parent = vertex;
}
}
}
}
public static <T> ArrayList<Vertex<T>> DijkstraWithGoal(Graph<T> graph, Vertex<T> source, Vertex<T> goal) {
if (source.equals(goal)) {
ArrayList<Vertex<T>> tmp = new ArrayList<Vertex<T>>();
tmp.add(source);
return tmp;
}
ArrayList<Vertex<T>> unfinishedVertices = new ArrayList<Vertex<T>>();
for (Vertex<T> vertex : graph.Vertices()) {
vertex.Distance = Integer.MAX_VALUE;
vertex.Parent = null;
unfinishedVertices.add(vertex);
}
source.Distance = 0;
while (unfinishedVertices.size() > 0) {
Vertex<T> vertex = GetClosestVertex(unfinishedVertices);
unfinishedVertices.remove(vertex);
if (vertex.equals(goal)) {
return GetPathToSource(vertex);
}
for (Vertex<T> adjVertex : graph.GetAdjacentVertices(vertex)) {
if (adjVertex.Distance > vertex.Distance + graph.GetEdgeWeight(vertex, adjVertex)) {
adjVertex.Distance = vertex.Distance + graph.GetEdgeWeight(vertex, adjVertex);
adjVertex.Parent = vertex;
}
}
}
return null;
}
private static <T> Vertex<T> GetClosestVertex(ArrayList<Vertex<T>> list) {
Vertex<T> candidate = list.get(0);
for (Vertex<T> vertex : list) {
if (vertex.Distance < candidate.Distance) {
candidate = vertex;
}
}
return candidate;
}
public static <T> ArrayList<Vertex<T>> GetPathToSource(Vertex<T> from) {
ArrayList<Vertex<T>> path = new ArrayList<Vertex<T>>();
Vertex<T> next = from;
while (next != null) {
path.add(next);
next = next.Parent;
}
return path;
}
}
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
System.out.println("===========Dijkstra=============");
Vertex<String> v1 = new Vertex<String>("v1");
Vertex<String> v2 = new Vertex<String>("v2");
Vertex<String> v3 = new Vertex<String>("v3");
Vertex<String> v4 = new Vertex<String>("v4");
Vertex<String> v5 = new Vertex<String>("v5");
Vertex<String> v6 = new Vertex<String>("v6");
ArrayList<Vertex<String>> vertices = new ArrayList<Vertex<String>>();
vertices.add(v1);
vertices.add(v2);
vertices.add(v3);
vertices.add(v4);
vertices.add(v5);
vertices.add(v6);
Graph<String> graph = new Graph<String>(vertices);
graph.CreateDirectedEdge(v1, v2, 5);
graph.CreateDirectedEdge(v5, v1, 5);
graph.CreateDirectedEdge(v5, v4, 1);
graph.CreateDirectedEdge(v4, v3, 1);
graph.CreateDirectedEdge(v2, v5, 2);
graph.CreateDirectedEdge(v3, v2, 1);
graph.CreateDirectedEdge(v4, v6, 2);
graph.CreateDirectedEdge(v3, v6, 2);
graph.CreateDirectedEdge(v6, v2, 3);
System.out.println(graph.toString());
//SearchAlgorithms.Dijkstra(graph, v4);
ArrayList<Vertex<String>> path = SearchAlgorithms.DijkstraWithGoal(graph, v5, v2);
System.out.println("Printing path from v5 to v2");
Collections.reverse(path);
for (Vertex<String> vertex : path) {
System.out.println(vertex);
}
}
}
===========Dijkstra=============
Graph:
v1 Edge to: v2(w=5.0)
v2 Edge to: v5(w=2.0)
v3 Edge to: v2(w=1.0) v6(w=2.0)
v4 Edge to: v3(w=1.0) v6(w=2.0)
v5 Edge to: v1(w=5.0) v4(w=1.0)
v6 Edge to: v2(w=3.0)
Printing path from v5 to v2
{Vertex} v5
{Vertex} v4
{Vertex} v3
{Vertex} v2
среда, 13 декабря 2017 г.
Depth First Search (DFS)
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
public class SearchAlgorithms {
public static <T> void DepthFirstSearch(Graph<T> graph, Vertex<T> sourceVertex, boolean reverseNeighbours) {
for (Vertex<T> vertex : graph.Vertices()) {
vertex.Parent = null;
vertex.Distance = 0;
vertex.Visited = false;
}
Stack<Vertex<T>> stack = new Stack<Vertex<T>>();
stack.push(sourceVertex);
while (stack.size() > 0) {
Vertex<T> vertex = stack.pop();
ArrayList<Vertex<T>> neighbours = graph.GetAdjacentVertices(vertex);
if (reverseNeighbours) {
Collections.reverse(neighbours);
//neighbours.Reverse();
}
for (Vertex<T> neighbour : neighbours) {
if (!neighbour.Visited) {
neighbour.Parent = vertex;
neighbour.Distance = vertex.Distance + graph.GetEdgeWeight(vertex, neighbour);
neighbour.Visited = true;
stack.push(neighbour);
}
}
vertex.Visited = true;
}
}
public static <T> ArrayList<Vertex<T>> DepthFirstSearchWithGoal(Graph<T> graph, Vertex<T> sourceVertex,
Vertex<T> goalVertex, boolean reverseNeighbours) {
if (sourceVertex.equals(goalVertex)) {
ArrayList<Vertex<T>> res = new ArrayList<Vertex<T>>();
res.add(sourceVertex);
return res;
}
for (Vertex<T> vertex : graph.Vertices()) {
vertex.Parent = null;
vertex.Distance = 0;
vertex.Visited = false;
}
Stack<Vertex<T>> stack = new Stack<Vertex<T>>();
stack.push(sourceVertex);
while (stack.size() > 0) {
Vertex<T> vertex = stack.pop();
ArrayList<Vertex<T>> neighbours = graph.GetAdjacentVertices(vertex);
if (reverseNeighbours) {
Collections.reverse(neighbours);
}
for (Vertex<T> neighbour : neighbours) {
if (!neighbour.Visited) {
neighbour.Parent = vertex;
neighbour.Distance = vertex.Distance + graph.GetEdgeWeight(vertex, neighbour);
neighbour.Visited = true;
if (neighbour.equals(goalVertex)) {
return GetPathToSource(neighbour);
}
stack.push(neighbour);
}
}
vertex.Visited = true;
}
// No path found
return null;
}
public static <T> ArrayList<Vertex<T>> GetPathToSource(Vertex<T> from) {
ArrayList<Vertex<T>> path = new ArrayList<Vertex<T>>();
Vertex<T> next = from;
while (next != null) {
path.add(next);
next = next.Parent;
}
return path;
}
}
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
System.out.println("===========BFS=============");
Vertex<String> v11 = new Vertex<String>("v1");
Vertex<String> v22 = new Vertex<String>("v2");
Vertex<String> v33 = new Vertex<String>("v3");
Vertex<String> v44 = new Vertex<String>("v4");
Vertex<String> v55 = new Vertex<String>("v5");
Vertex<String> v66 = new Vertex<String>("v6");
Vertex<String> v77 = new Vertex<String>("v7");
ArrayList<Vertex<String>> vertices1 = new ArrayList<Vertex<String>>();
//vertices.clear();
vertices1.add(v11);
vertices1.add(v22);
vertices1.add(v33);
vertices1.add(v44);
vertices1.add(v55);
vertices1.add(v66);
vertices1.add(v77);
Graph<String> graph1 = new Graph<String>(vertices1);
graph1.CreateUndirectedEdge(v44, v55, 0);
graph1.CreateUndirectedEdge(v44, v22, 0);
graph1.CreateUndirectedEdge(v44, v11, 0);
graph1.CreateUndirectedEdge(v55, v66, 0);
graph1.CreateUndirectedEdge(v22, v55, 0);
graph1.CreateUndirectedEdge(v22, v77, 0);
graph1.CreateUndirectedEdge(v22, v11, 0);
graph1.CreateUndirectedEdge(v11, v33, 0);
graph1.CreateUndirectedEdge(v11, v77, 0);
graph1.CreateUndirectedEdge(v77, v66, 0);
System.out.println(graph1.toString());
SearchAlgorithms.BreadthFirstSearch(graph1, v44);
ArrayList<Vertex<String>> fromV6 = SearchAlgorithms.GetPathToSource(v66);
System.out.println("Printing path from v6");
for (Vertex<String> vertex : fromV6) {
System.out.println(vertex);
}
ArrayList<Vertex<String>> path = SearchAlgorithms.BreadthFirstSearchWithGoal(graph1, v66, v33);
System.out.println("Printing path from v3 to v6");
for (Vertex<String> vertex : path) {
System.out.println(vertex);
}
System.out.println("===========DFS=============");
System.out.println(graph1.toString());
SearchAlgorithms.DepthFirstSearch(graph1, v44, true);
ArrayList<Vertex<String>> fromV66 = SearchAlgorithms.GetPathToSource(v66);
System.out.println("Printing path from v6");
for (Vertex<String> vertex : fromV66) {
System.out.println(vertex);
}
ArrayList<Vertex<String>> path1 = SearchAlgorithms.DepthFirstSearchWithGoal(graph1, v66, v33, true);
System.out.println("Printing path from v3 to v6");
for (Vertex<String> vertex : path1) {
System.out.println(vertex);
}
}
}
Graph:
v1 Edge to: v2(w=1.0) v3(w=1.0) v4(w=1.0) v7(w=1.0)
v2 Edge to: v1(w=1.0) v4(w=1.0) v5(w=1.0) v7(w=1.0)
v3 Edge to: v1(w=1.0)
v4 Edge to: v1(w=1.0) v2(w=1.0) v5(w=1.0)
v5 Edge to: v2(w=1.0) v4(w=1.0) v6(w=1.0)
v6 Edge to: v5(w=1.0) v7(w=1.0)
v7 Edge to: v1(w=1.0) v2(w=1.0) v6(w=1.0)
Printing path from v6
{Vertex} v6
{Vertex} v7
{Vertex} v1
{Vertex} v4
Printing path from v3 to v6
{Vertex} v3
{Vertex} v1
{Vertex} v2
{Vertex} v5
{Vertex} v6
Подписаться на:
Сообщения (Atom)