added all past projects

This commit is contained in:
Hannes
2017-11-10 00:13:57 +01:00
parent 5f63f0c599
commit 8c94608805
1391 changed files with 109456 additions and 0 deletions

View File

@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<fileset-config file-format-version="1.2.0" simple-config="true" sync-formatter="false">
<fileset name="all" enabled="true" check-config-name="Programmieren_WS_2015" local="false">
<file-match-pattern match-pattern="." include-pattern="true"/>
</fileset>
</fileset-config>

View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="output" path="bin"/>
</classpath>

View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Assignment3C_Routing</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Assignment3C_Routing</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

View File

@@ -0,0 +1,11 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8

View File

@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/bin" />
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="jdk" jdkName="JavaSE-1.8" jdkType="JavaSDK" />
</component>
</module>

View File

@@ -0,0 +1,7 @@
1 0
1 2
2 0
0 2
2 3
3 2
0 3

View File

@@ -0,0 +1,57 @@
package edu.kit.informatik;
/**
* The Class Edge.
*
* @author Hannes Kuchelmeister
* @version 1.0
*/
public class Edge {
/** The vertex the edge begins at. */
private final Vertex fromVertex;
/** The vertex the edge ends at. */
private final Vertex toVertex;
/**
* Instantiates a new edge.
*
* @param from
* the vertex the edge begins at
* @param to
* the vertex the edge ends at
*/
public Edge(final Vertex from, final Vertex to) {
this.fromVertex = from;
this.toVertex = to;
}
/**
* Gets vertex the edge begins at.
*
* @return the vertex the edge begins at
*/
public Vertex getFromVertex() {
return fromVertex;
}
/**
* Gets the vertex the edge ends at.
*
* @return the vertex the edge ends at
*/
public Vertex getToVertex() {
return toVertex;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "(" + fromVertex.toString() + " , " + toVertex.toString() + ")";
}
}

View File

@@ -0,0 +1,189 @@
package edu.kit.informatik;
/**
* The Class Graph.
*
* @author Hannes Kuchelmeister
* @version 1.0
*/
public class Graph {
/** The edges. */
private Edge[] edges;
/** The vertices. */
private Vertex[] vertices;
/**
* Instantiates a new graph.
*/
public Graph() {
this.edges = null;
this.vertices = null;
}
/**
* Instantiates a new graph. (Adds all vertices of the edges to the vertices
* array)
*
* @param edges
* the edges of the graph
*/
public Graph(final Edge[] edges) {
this.edges = edges;
this.vertices = null;
if (this.edges != null) {
for (final Edge edge : this.edges) {
if (!this.contains(edge.getFromVertex()))
this.add(edge.getFromVertex());
if (!this.contains(edge.getToVertex()))
this.add(edge.getToVertex());
if (!this.contains(edge)) {
this.add(edge);
}
}
}
}
/**
* Adds an edge to the graph and the vertices it contains.
*
* @param edge
* the edge
*/
public void add(final Edge edge) {
if (edge == null)
return;
if (edges == null)
edges = new Edge[1];
else {
final Edge[] tmpEdge = edges.clone();
edges = new Edge[tmpEdge.length + 1];
System.arraycopy(tmpEdge, 0, edges, 0, tmpEdge.length);
}
if (!this.contains(edge)) {
edges[edges.length - 1] = edge;
this.add(edge.getFromVertex());
this.add(edge.getToVertex());
}
}
/**
* Adds a vertex to the graph.
*
* @param vertex
* the vertex
*/
public void add(final Vertex vertex) {
if (vertex == null)
return;
if (vertices == null) {
vertices = new Vertex[1];
vertices[0] = vertex;
} else {
if (!this.contains(vertex)) {
final Vertex[] tmpVertex = vertices.clone();
vertices = new Vertex[tmpVertex.length + 1];
System.arraycopy(tmpVertex, 0, vertices, 0, tmpVertex.length);
vertices[vertices.length - 1] = vertex;
}
}
}
/**
* Gets the number of connections.
*
* @param i
* the id of the vertex the route starts at
* @param j
* the id of the vertex the route ends at
* @param pathlength
* the path length of the route
* @return the number of connections
*/
public int getNumberOfConnections(final int i, final int j, final int pathlength) {
if (pathlength < 1 || vertices == null || i >= vertices.length || j >= vertices.length)
return 0;
Matrix resultMatrix = this.toMatrix();
for (int k = 1; k < pathlength; k++) {
resultMatrix = Matrix.crossProduct(resultMatrix, resultMatrix);
}
return resultMatrix.getSingleElement(i, j);
}
/**
* Checks if this graph contains the edge.
*
* @param edge
* the edge
* @return true, if contained
*/
public boolean contains(final Edge edge) {
if (edges != null) {
for (final Edge e : edges) {
if (e != null && e.equals(edge))
return true;
}
}
return false;
}
/**
* Checks if this graph contains the vertex.
*
* @param vertex
* the vertex
* @return true, if contained
*/
public boolean contains(final Vertex vertex) {
if (vertices != null) {
for (final Vertex v : vertices) {
if (v != null && v.equals(vertex))
return true;
}
}
return false;
}
/**
* Converts the graph to an adjacency matrix.
*
* @return the adjacency matrix
*/
public Matrix toMatrix() {
if (vertices == null || edges == null)
return null;
final Matrix result = new Matrix(vertices.length, vertices.length);
for (final Edge edge : edges) {
if (edge != null)
result.setSingleElement(edge.getFromVertex().getId(), edge.getToVertex().getId(), 1);
}
return result;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
String out = "E = { ";
if (edges != null) {
for (final Edge edge : this.edges) {
out += edge.toString() + " ";
}
}
out += "}\nV = { ";
if (vertices != null) {
for (final Vertex vertex : vertices) {
out += vertex.toString() + " ";
}
}
out += "}";
return out;
}
}

View File

@@ -0,0 +1,84 @@
package edu.kit.informatik;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
/**
* The Class Main.
*
* @author Hannes Kuchelmeister
* @version 1.0
*/
public final class Main {
/** The Constant FILENOTFOUND for fileNotFound exceptions. */
private static final String FILENOTFOUND = "File not Found";
/** The Constant IO for IO exceptions. */
private static final String IO = "IOException occured";
/**
* Instantiates a new main.
*/
private Main() {
}
/**
* Checks if 4 arguments are handed over and calculates the number of
* connection between two nodes (args[1] and args[2]) at the pathlength
* (args[3]) and prints them to the console.
*
* @param args
* the arguments
*/
public static void main(final String[] args) {
if (args.length == 4) {
final String filePath = args[0];
final int startVertexId = Integer.parseInt(args[1]);
final int endVertexId = Integer.parseInt(args[2]);
final int pathLength = Integer.parseInt(args[3]);
final Graph graph = Main.readFile(filePath);
System.out.println(graph.getNumberOfConnections(startVertexId, endVertexId, pathLength));
}
}
/**
* Reads a file that contains the description for a graph.
*
* @param path
* the path of the file
* @return the graph
*/
private static Graph readFile(final String path) {
FileReader in = null;
try {
in = new FileReader(path);
} catch (final FileNotFoundException e) {
System.out.println(FILENOTFOUND);
System.exit(1);
}
final BufferedReader reader = new BufferedReader(in);
try {
final Graph graph = new Graph();
String line = reader.readLine();
while (line != null) {
final String[] nums = line.trim().split("\\s+");
if (nums.length == 2)
graph.add(new Edge(new Vertex(Integer.parseInt(nums[0])), new Vertex(Integer.parseInt(nums[1]))));
line = reader.readLine();
}
return graph;
} catch (final IOException e) {
System.out.println(IO);
System.exit(1);
}
return null;
}
}

View File

@@ -0,0 +1,204 @@
package edu.kit.informatik;
/**
* The Class Matrix.
*
* @author Hannes Kuchelmeister
* @version 1.0
*/
public class Matrix {
/**
* The matrix. (represents the data of the matrix ([row][column]) starting
* with (0,0))
*/
private int[][] data;
/**
* Instantiates a new matrix of which every element is 0
*
* @param iElements
* the number of rows
* @param jElements
* the number of columns
*/
public Matrix(int iElements, int jElements) {
data = new int[iElements][jElements];
for (int i = 0; i < iElements; i++) {
for (int j = 0; j < jElements; j++) {
data[i][j] = 0;
}
}
}
/**
* Instantiates a new matrix from a data array
*
* @param data
* the data of the matrx
*/
public Matrix(int[][] data) {
this.data = data;
}
/**
* Gets a single element of the matrix at the position (row,column).
*
* @param i
* the row
* @param j
* the column
* @return the element at (i,j)
*/
public int getSingleElement(int i, int j) {
if (i < data.length && j < data[i].length)
return data[i][j];
else
return -1;
}
/**
* Sets a single element of the matrix to a value
*
* @param i
* row of the element
* @param j
* column of the element
* @param val
* the value that should be set at (i,j)
*/
public void setSingleElement(int i, int j, int val) {
if (i < data.length && j < data[i].length) {
data[i][j] = val;
}
}
/**
* Gets the number of columns.
*
* @return the number of columns
*/
public int getColumnCount() {
return data[0].length;
}
/**
* Gets the number of rows.
*
* @return the number of rows
*/
public int getRowCount() {
return data.length;
}
/**
* Gets a row as vector.
*
* @param row
* the row that should be returned as vector
* @return the row converted to a vector
*/
public VectorInt getRowVector(int row) {
int[] arr = new int[data[row].length];
for (int i = 0; i < arr.length; i++) {
arr[i] = data[row][i];
}
return new VectorInt(arr);
}
/**
* Gets a column vector.
*
* @param column
* the column that should be returned as vector
* @return the column converted to a vector
*/
public VectorInt getColumnVector(int column) {
int[] arr = new int[data.length];
for (int i = 0; i < arr.length; i++) {
arr[i] = data[i][column];
}
return new VectorInt(arr);
}
/**
* Addition of two matrices.
*
* @param m
* the first matrix
* @param n
* the second matrix
* @return the result
*/
public static Matrix addition(Matrix m, Matrix n) {
if (n.getColumnCount() != m.getColumnCount() && n.getRowCount() != m.getColumnCount())
return null;
int[][] result = new int[n.getRowCount()][n.getColumnCount()];
for (int i = 0; i < result.length; i++) {
for (int j = 0; j < result[i].length; j++) {
result[i][j] = n.getSingleElement(i, j) + m.getSingleElement(i, j);
}
}
return new Matrix(result);
}
/**
* Cross product of two matrices. (m will be multiplied by n [n x m])
*
* @param n
* the second matrix
* @param m
* the first matrix
* @return the result
*/
public static Matrix crossProduct(Matrix n, Matrix m) { // n x m
if (n.getColumnCount() != m.getRowCount())
return null;
int[][] result = new int[n.getRowCount()][m.getColumnCount()];
for (int i = 0; i < result.length; i++) {
for (int j = 0; j < result[i].length; j++) {
// Skalarprodukt
result[i][j] = VectorInt.scalarProduct(n.getRowVector(i), m.getColumnVector(j));
}
}
return new Matrix(result);
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (obj instanceof Matrix && ((Matrix) obj).getColumnCount() == this.getColumnCount()
&& ((Matrix) obj).getRowCount() == this.getRowCount()) {
for (int i = 0; i < this.data.length; i++) {
for (int j = 0; j < this.data[i].length; j++) {
if (this.data[i][j] != ((Matrix) obj).getSingleElement(i, j))
return false;
}
}
return true;
}
return false;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
String str = "";
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[i].length; j++) {
str += " " + String.format("% 4d", this.data[i][j]);
}
str += "\n";
}
return str;
}
}

View File

@@ -0,0 +1,59 @@
package edu.kit.informatik;
/**
* The Class VectorInt.
*
* @author Hannes Kuchelmeister
* @version 1.0
*/
public class VectorInt {
/** The data. */
private int[] data;
/**
* Instantiates a new vector int.
*
* @param vec
* the data of the vector
*/
public VectorInt(int[] vec) {
this.data = vec;
};
/**
* Scalar product of two vectors.
*
* @param vec1
* the first vector
* @param vec2
* the second vector
* @return the scalar product
*/
public static int scalarProduct(VectorInt vec1, VectorInt vec2) {
if (vec1.data.length != vec2.data.length)
return 0;
int sum = 0;
for (int i = 0; i < vec1.data.length; i++) {
sum += vec1.data[i] * vec2.data[i];
}
return sum;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (obj instanceof VectorInt) {
for (int i = 0; i < data.length; i++) {
if (data[i] != ((VectorInt) obj).data[i])
return false;
}
return true;
}
return false;
}
}

View File

@@ -0,0 +1,55 @@
package edu.kit.informatik;
/**
* The Class Vertex.
*
* @author Hannes Kuchelmeister
* @version 1.0
*/
public class Vertex {
/** The id. */
private int id;
/**
* Instantiates a new vertex.
*
* @param id
* the id of the vertex
*/
public Vertex(int id) {
this.id = id;
}
/**
* Gets the id of the vertex
*
* @return the id
*/
public int getId() {
return id;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "" + id + "";
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if ((obj instanceof Vertex) && (((Vertex) obj).id == this.id))
return true;
else
return false;
}
}

View File

@@ -0,0 +1,7 @@
1 0
1 2
2 0
0 2
2 3
3 2
0 3