Advertisement
Sylv3rWolf

AiZLab7

Nov 16th, 2015
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.07 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package aiz7;
  7.  
  8. /**
  9.  *
  10.  * @author MaxSylverWolf
  11.  */
  12. import java.util.ArrayDeque;
  13. import java.util.ArrayList;
  14. import java.util.Deque;
  15. import java.util.List;
  16.  
  17. // GRAFY NIESKIEROWANE
  18. class Graph {
  19. // liczba krawedzi
  20. private int e;
  21. // liczba wierzcholkow
  22. private int v;
  23. // tablica list sasiedztwa danego wierzcholka
  24. private List<Integer>[] adjacencyList;
  25.  
  26. @SuppressWarnings("unchecked")
  27. public Graph(int v) {
  28. this.v = v;
  29. this.e = 0;
  30. adjacencyList = (List<Integer>[]) new List[v];
  31. for (int i = 0; i < v; i++) {
  32. adjacencyList[i] = new ArrayList<Integer>();
  33. }
  34. }
  35.  
  36. public void addEdge(int v, int w) {
  37. adjacencyList[v].add(w);
  38. adjacencyList[w].add(v);
  39. e++;
  40. }
  41.  
  42. public int getNumberOfEdges() {
  43. return e;
  44. }
  45.  
  46. public int getNumberOfVertices() {
  47. return v;
  48. }
  49.  
  50. public Iterable<Integer> getAdjacencyList(int v) {
  51. return adjacencyList[v];
  52. }
  53.  
  54. public String toString() {
  55. StringBuilder s = new StringBuilder();
  56. String newLine = System.getProperty("line.separator");
  57. s.append("wierzcholki: ").append(v).append("; krawedzie: ").append(e)
  58. .append(newLine);
  59. for (int i = 0; i < v; i++) {
  60. s.append(i).append(": ");
  61. for (int w : adjacencyList[i]) {
  62. s.append(w).append(" ");
  63. }
  64. s.append(newLine);
  65. }
  66. return s.toString();
  67. }
  68. }
  69.  
  70. class DFSPaths {
  71.  
  72. private int[] edgeTo;
  73. // tablica odwiedzonych wierzcholkow
  74. private boolean[] marked;
  75. // wierzcholek zrodlowy, z ktorego rozpoczynamy przeszukiwanie
  76. private final int source;
  77.  
  78. public DFSPaths(Graph graph, int source) {
  79. this.source = source;
  80. edgeTo = new int[graph.getNumberOfVertices()];
  81. marked = new boolean[graph.getNumberOfVertices()];
  82. dfs(graph, source);
  83. }
  84.  
  85. public boolean hasPathTo(int vertex) {
  86. return marked[vertex];
  87. }
  88.  
  89. public Iterable<Integer> getPathTo(int vertex) {
  90. Deque<Integer> path = new ArrayDeque<Integer>();
  91. // jesli nie istnieje sciezka zwroc pusta sciezke
  92. if (!hasPathTo(vertex)) {
  93. return path;
  94. }
  95. // dopoki istnieje wierzcholek dodawaj go do stosu
  96. for (int w = vertex; w != source; w = edgeTo[w]) {
  97. path.push(w);
  98. }
  99. // dodaj na koniec krawedz zrodlowa
  100. path.push(source);
  101. return path;
  102. }
  103.  
  104. private void dfs(Graph graph, int vertex) {
  105. //  wierzcholek jako odwiedzony
  106. marked[vertex] = true;
  107.  
  108. for (int w : graph.getAdjacencyList(vertex)) {
  109. if (!marked[w]) {
  110. edgeTo[w] = vertex;
  111. dfs(graph, w);
  112. }
  113. }
  114. }
  115. }
  116.  
  117. // GRAFY SKIEROWANE
  118. class DirectedGraph {
  119. // liczba krawedzi
  120. private int e;
  121. // liczba wierzcholkow
  122. private int v;
  123. // tablica list sasiedztwa danego wierzcholka
  124. private List<Integer>[] adjacencyList;
  125.  
  126. @SuppressWarnings("unchecked")
  127. public DirectedGraph(int v) {
  128. this.v = v;
  129. this.e = 0;
  130. adjacencyList = (List<Integer>[]) new List[v];
  131. for (int i = 0; i < v; i++) {
  132. adjacencyList[i] = new ArrayList<Integer>();
  133. }
  134. }
  135.  
  136. public void addEdge(int v, int w) {
  137. adjacencyList[v].add(w);
  138. e++;
  139. }
  140.  
  141. public int getNumberOfEdges() {
  142. return e;
  143. }
  144.  
  145. public int getNumberOfVertices() {
  146. return v;
  147. }
  148.  
  149. public Iterable<Integer> getAdjacencyList(int v) {
  150. return adjacencyList[v];
  151. }
  152.  
  153. public String toString() {
  154. StringBuilder s = new StringBuilder();
  155. String newLine = System.getProperty("line.separator");
  156. s.append("wierzcholki: ").append(v).append("; krawedzie: ").append(e)
  157. .append(newLine);
  158. for (int i = 0; i < v; i++) {
  159. s.append(i).append(": ");
  160. for (int w : adjacencyList[i]) {
  161. s.append(w).append(" ");
  162. }
  163. s.append(newLine);
  164. }
  165. return s.toString();
  166. }
  167. }
  168.  
  169. class DFSDirectedPaths {
  170.  
  171. private int[] edgeTo;
  172. // tablica odwiedzonych wierzcholkow
  173. private boolean[] marked;
  174. // wierzcholek zrodlowy, z ktorego rozpoczynamy przeszukiwanie
  175. private final int source;
  176.  
  177. public DFSDirectedPaths(DirectedGraph graph, int source) {
  178. this.source = source;
  179. edgeTo = new int[graph.getNumberOfVertices()];
  180. marked = new boolean[graph.getNumberOfVertices()];
  181. dfs(graph, source);
  182. }
  183.  
  184.  
  185. public boolean hasPathTo(int vertex) {
  186. return marked[vertex];
  187. }
  188.  
  189. public Iterable<Integer> getPathTo(int vertex) {
  190. Deque<Integer> path = new ArrayDeque<Integer>();
  191. // jesli nie istnieje sciezka zwroc pusta sciezke
  192. if (!hasPathTo(vertex)) {
  193. return path;
  194. }
  195. // dopoki istnieje wierzcholek dodawaj go do stosu
  196. for (int w = vertex; w != source; w = edgeTo[w]) {
  197. path.push(w);
  198. }
  199. // dodaj na koniec krawedz zrodlowa
  200. path.push(source);
  201. return path;
  202. }
  203.  
  204. private void dfs(DirectedGraph graph, int vertex) {
  205. // oznaczamy wierzcholek jako odwiedzony
  206. marked[vertex] = true;
  207. for (int w : graph.getAdjacencyList(vertex)) {
  208. if (!marked[w]) {
  209. edgeTo[w] = vertex;
  210. dfs(graph, w);
  211. }
  212. }
  213. }
  214. }
  215.  
  216.  
  217. class Paths {
  218.  
  219. public static void main(String[] args) {
  220.  
  221. Graph graph = new Graph(6);
  222.  
  223. graph.addEdge(0, 1);
  224. graph.addEdge(0, 2);
  225.  
  226. graph.addEdge(1, 4);
  227.  
  228. graph.addEdge(2, 3);
  229. graph.addEdge(2, 5);
  230.  
  231. graph.addEdge(3, 0);
  232.  
  233. graph.addEdge(4, 3);
  234.  
  235. System.out.println("Graf nieskierowany: " + graph);
  236.  
  237. System.out.println("\nPrzeszukiwanie grafu w głąb - sciezki nieskierowane");
  238. // droga z 1 do 5
  239. DFSPaths dfs1 = new DFSPaths(graph, 0);
  240. for (int it : dfs1.getPathTo(4)) {
  241. System.out.print(it + " ");
  242. }
  243. System.out.println("\n----------");
  244.  
  245. // droga z 5 do 1
  246. DFSPaths dfs2 = new DFSPaths(graph, 4);
  247. for (int it : dfs2.getPathTo(0)) {
  248. System.out.print(it + " ");
  249. }
  250.  
  251. // --------------------------------------------
  252. // grafy skierowane
  253. DirectedGraph digraph = new DirectedGraph(6);
  254.  
  255. digraph.addEdge(0, 1);
  256. digraph.addEdge(0, 2);
  257.  
  258. digraph.addEdge(1, 4);
  259.  
  260. digraph.addEdge(2, 3);
  261. digraph.addEdge(2, 5);
  262.  
  263. digraph.addEdge(3, 0);
  264.  
  265. digraph.addEdge(4, 3);
  266.  
  267. System.out.println("\n\nGraf skierowany: " + digraph);
  268.  
  269. System.out.println("\nPrzeszukiwanie grafu w głąb - sciezki skierowane");
  270. // droga z 1 do 5
  271. DFSDirectedPaths dfs3 = new DFSDirectedPaths(digraph, 0);
  272. for (int it : dfs3.getPathTo(4)) {
  273. System.out.print(it + " ");
  274. }
  275. System.out.println("\n----------");
  276.  
  277. // droga z 5 do 1
  278. DFSDirectedPaths dfs4 = new DFSDirectedPaths(digraph, 4);
  279. for (int it : dfs4.getPathTo(0)) {
  280. System.out.print(it + " ");
  281. }
  282. }
  283. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement