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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# AlgorithmsAndDataStructures
1. Реализовать программу, в которой задается граф из 10 вершин.
Задать ребра и найти кратчайший путь с помощью поиска в ширину.
51 changes: 51 additions & 0 deletions src/algorithmsAndDataStructures/BreadthFirstPath.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package algorithmsAndDataStructures;

import java.util.LinkedList;

public class BreadthFirstPath {

private boolean[] marked;
private int[] edgTo;
private int source;

public BreadthFirstPath(Graph graph, int source) {
this.source = source;
marked = new boolean[graph.getVertexCount()];
edgTo = new int[graph.getVertexCount()];
bfs(graph, source);
}

private void bfs(Graph graph, int source) {
LinkedList<Integer> queue = new LinkedList<>();
queue.add(source);
marked[source] = true;
while (!queue.isEmpty()) {
int vertex = queue.removeFirst();
for (int w : graph.getAdjLists(vertex)) {
if (!marked[w]) {
marked[w] = true;
edgTo[w] = vertex;
queue.addLast(w);
}
}
}
}


public boolean hasPathTo(int v) {
return marked[v];
}

public LinkedList<Integer> pathTo(int v) {
if (!hasPathTo(v)) {
return null;
}
LinkedList<Integer> stack = new LinkedList<>();
int vertex = v;
while (vertex != source) {
stack.push(vertex);
vertex = edgTo[vertex];
}
return stack;
}
}
44 changes: 44 additions & 0 deletions src/algorithmsAndDataStructures/DepthFirstPath.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package algorithmsAndDataStructures;

import java.util.LinkedList;

public class DepthFirstPath {
private boolean[] marked;
private int[] edgTo;
private int source;

public DepthFirstPath(Graph graph, int source) {
this.source = source;
marked = new boolean[graph.getVertexCount()];
edgTo = new int[graph.getVertexCount()];
dfs(graph, source);

}

private void dfs(Graph graph, int v) {
marked[v] = true;
for (int w : graph.getAdjLists(v)) {
if (!marked[w]) {
edgTo[w] = v;
dfs(graph, w);
}
}
}

public boolean hasPathTo(int v) {
return marked[v];
}

public LinkedList<Integer> pathTo(int v){
if (!hasPathTo(v)){
return null;
}
LinkedList<Integer>stack=new LinkedList<>();
int vertex=v;
while (vertex!=source){
stack.push(vertex);
vertex=edgTo[vertex];
}
return stack;
}
}
44 changes: 44 additions & 0 deletions src/algorithmsAndDataStructures/Graph.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package algorithmsAndDataStructures;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class Graph {
private int vertexCount;
private int edgeCount;
private LinkedList<Integer>[] adjLists;

public Graph(int vertexCount) {
if (vertexCount < 1) {
throw new IllegalArgumentException("vertexCount: " + vertexCount);
}
this.vertexCount = vertexCount;
adjLists = new LinkedList[vertexCount];
for (int i = 0; i < adjLists.length; i++) {
adjLists[i] = new LinkedList<>();
}
}

public int getVertexCount() {
return vertexCount;
}

public int getEdgeCount() {
return edgeCount;
}

public List<Integer> getAdjLists(int vertex) {
return Collections.unmodifiableList(adjLists[vertex]);
}

public void addEdge(int v1, int v2) {
if (v1 < 0 || v2 < 0 || v1 > vertexCount || v2 > vertexCount) {
throw new IllegalArgumentException("");
}
adjLists[v1].add(v2);
if (v1 != v2) {
adjLists[v2].add(v1);
}
}
}
15 changes: 14 additions & 1 deletion src/algorithmsAndDataStructures/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@
public class Main {

public static void main(String[] args) {
// write your code here
Graph graph = new Graph(10);
graph.addEdge(1, 2);
graph.addEdge(2, 3);
graph.addEdge(3, 4);
graph.addEdge(5, 6);
graph.addEdge(2, 5);
graph.addEdge(7, 6);
graph.addEdge(8, 1);
graph.addEdge(2, 9);

BreadthFirstPath bfp = new BreadthFirstPath(graph, 1);
System.out.println(bfp.pathTo(7));


}
}