├── README.md ├── src ├── node_data.java ├── edge_data.java ├── Node.java ├── Edge.java ├── Revers_Kruskal.java ├── EulerCycle.java ├── TreeIsomorphism.java ├── Dfs.java ├── Graph.java ├── Haffman.java ├── dijkstra.java ├── Kruskal.java ├── BucketsProblem.java └── BFS.java ├── .idea ├── dictionaries │ └── amichaihadad.xml ├── codeStyles │ ├── codeStyleConfig.xml │ └── Project.xml ├── vcs.xml ├── modules.xml └── misc.xml └── Algo 2.iml /README.md: -------------------------------------------------------------------------------- 1 | # Algo-2 2 | -------------------------------------------------------------------------------- /src/node_data.java: -------------------------------------------------------------------------------- 1 | public interface node_data { 2 | public int getId(); 3 | 4 | } 5 | -------------------------------------------------------------------------------- /src/edge_data.java: -------------------------------------------------------------------------------- 1 | public interface edge_data { 2 | public int getSrc(); 3 | public int getDst(); 4 | 5 | } 6 | -------------------------------------------------------------------------------- /.idea/dictionaries/amichaihadad.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/codeStyles/codeStyleConfig.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /src/Node.java: -------------------------------------------------------------------------------- 1 | public class Node implements node_data { 2 | int id; 3 | public Node(int id){ 4 | this.id = id; 5 | } 6 | 7 | public Node(node_data v) { 8 | this.id = v.getId(); 9 | } 10 | 11 | @Override 12 | public int getId() { 13 | return this.id; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/Edge.java: -------------------------------------------------------------------------------- 1 | public class Edge implements edge_data { 2 | private int src; 3 | private int dst; 4 | public Edge(int src, int dst){ 5 | this.dst = dst; 6 | this.src = src; 7 | } 8 | @Override 9 | public int getSrc() { 10 | return src; 11 | } 12 | 13 | @Override 14 | public int getDst() { 15 | return dst; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Algo 2.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /src/Revers_Kruskal.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Comparator; 3 | import java.util.Iterator; 4 | import java.util.PriorityQueue; 5 | 6 | public class Revers_Kruskal { 7 | public static ArrayList find_min_span_Tree(Graph g) { 8 | ArrayList arrayList = new ArrayList<>(); 9 | ArrayList Tree = new ArrayList<>(); 10 | for (int i = 1; i < g.size(); i++) { 11 | for (Iterator it = g.getEdges(i).iterator(); it.hasNext(); ) { 12 | edge_data e = it.next(); 13 | arrayList.add(e); 14 | } 15 | } 16 | arrayList.sort((edge_data d, edge_data x) -> x.getWeight() - d.getWeight()); 17 | int i = 0; 18 | while (Tree.size() < g.size()-1){ 19 | edge_data t =arrayList.get(i); 20 | g.removeEdge(t.getSrc(),t.getDst()); 21 | BFS bfs = new BFS(g); 22 | if (!bfs.connected()){ 23 | Tree.add(t); 24 | } 25 | i++; 26 | } 27 | return Tree; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/EulerCycle.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Stack; 3 | 4 | public class EulerCycle { 5 | private Graph graph; 6 | public EulerCycle(Graph g){ 7 | this.graph = new Graph(g); 8 | } 9 | public ArrayList findCycle(){ 10 | if (!isEuler()){ 11 | return null; 12 | } 13 | Stack s = new Stack<>(); 14 | ArrayList arrayList = new ArrayList(); 15 | s.push(1); 16 | while (!s.isEmpty()){ 17 | int u = s.peek(); 18 | if (graph.deg(u) == 0){ 19 | s.pop(); 20 | arrayList.add(u); 21 | } 22 | else { 23 | int v = graph.getEdges(u).iterator().next().getDst();// 24 | s.push(v); 25 | graph.removeEdge(u,v); 26 | } 27 | } 28 | return arrayList; 29 | } 30 | private boolean isEuler() { 31 | return true; 32 | } 33 | public static void main(String[] args){ 34 | Graph g = new Graph(); 35 | for (int i = 0;i<6;i++){ 36 | g.addNode(); 37 | } 38 | g.add2Edge(1,3); 39 | g.add2Edge(1,6); 40 | g.add2Edge(1,2); 41 | g.add2Edge(6,2); 42 | g.add2Edge(1,5); 43 | g.add2Edge(2,3); 44 | g.add2Edge(2,5); 45 | g.add2Edge(3,5); 46 | g.add2Edge(3,4); 47 | g.add2Edge(5,4); 48 | System.out.println("Euler Circle: " + new EulerCycle(g).findCycle().toString()); // Euler Circle: [1, 5, 4, 3, 5, 2, 6, 1, 2, 3, 1] 49 | } 50 | } 51 | 52 | -------------------------------------------------------------------------------- /src/TreeIsomorphism.java: -------------------------------------------------------------------------------- 1 | import java.util.LinkedList; 2 | import java.util.Queue; 3 | import java.util.PriorityQueue; 4 | public class TreeIsomorphism { 5 | class Node implements Comparable{ 6 | Integer length; 7 | int index; 8 | Node(int index,int length){ 9 | this.index = index; 10 | this.length = length; 11 | } 12 | 13 | public void setLength(Integer length) { 14 | this.length = length; 15 | } 16 | 17 | public int getIndex() { 18 | return index; 19 | } 20 | 21 | @Override 22 | public int compareTo(Node o) { 23 | return o.length-length; 24 | } 25 | } 26 | 27 | public String toCode(int[] tree, int root){ 28 | String[] ans = new String[tree.length]; 29 | AHU(ans,tree,root,null); 30 | return ans[root]; 31 | } 32 | public void AHU(String[] phi, int[] tree, int v,PriorityQueue insert_me){ 33 | if (isLeaf(tree,v)){ 34 | insert_me.add(new Node(v,2)); 35 | phi[v] = "10"; 36 | return; 37 | } 38 | PriorityQueue queue1 = new PriorityQueue<>(); 39 | for (int i = 0; i stack = new Stack<>(); 27 | Iterator[] iters = new Iterator[n]; 28 | for (int i=1; i NMap = new LinkedHashMap<>(); 9 | private HashMap> EMap = new LinkedHashMap<>(); 10 | private int sizeE = 0; 11 | public Graph(){} 12 | public Graph(Graph copy){ 13 | for (node_data v: copy.NMap.values()) { 14 | this.NMap.put(v.getId(), new Node(v)); 15 | } 16 | for (node_data v: copy.NMap.values()){ 17 | if (copy.getEdges(v.getId())!=null) 18 | for (edge_data e:copy.getEdges(v.getId())) 19 | add1Edge(e.getSrc(),e.getDst()); 20 | } 21 | this.id = copy.id; 22 | this.sizeE = copy.sizeE; 23 | } 24 | 25 | public void addNode(){ 26 | NMap.put(id,new Node(id)); 27 | id++; 28 | } 29 | public void add2Edge(int src, int dst){ 30 | sizeE++; 31 | add1Edge(src,dst); 32 | add1Edge(dst,src); 33 | } 34 | private void add1Edge(int src, int dst){ 35 | if (NMap.get(src) == null || NMap.get(dst) == null){ 36 | System.out.println("dst or src dont exist"); 37 | return; 38 | } 39 | if (EMap.get(src) == null) { 40 | HashMap temp = new LinkedHashMap<>(); 41 | temp.put(dst,new Edge(src,dst)); 42 | EMap.put(src, temp); 43 | } 44 | else EMap.get(src).put(dst,new Edge(src,dst)); 45 | } 46 | public Collection getEdges(int id){ 47 | if (EMap.get(id) != null) 48 | return EMap.get(id).values(); 49 | return null; 50 | } 51 | 52 | public int size() { 53 | return id; 54 | } 55 | 56 | public int getSizeE() { 57 | return sizeE; 58 | } 59 | public boolean isLeaf(int id){ 60 | Collection temp = getEdges(id); 61 | return temp == null || temp.size() == 1; 62 | } 63 | public node_data removeNode(int id){ 64 | node_data temp = NMap.remove(id); 65 | EMap.remove(id); 66 | for (HashMap lists : EMap.values()){ 67 | lists.remove(id); 68 | } 69 | return temp; 70 | } 71 | 72 | public int deg(int u) { 73 | if(getEdges(u)==null) return 0; 74 | return getEdges(u).size(); 75 | } 76 | 77 | public void removeEdge(int u, int v) { 78 | if (EMap.get(u) != null) 79 | EMap.get(u).remove(v); 80 | if (EMap.get(v) != null) 81 | EMap.get(v).remove(u); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /src/Haffman.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | import java.util.Comparator; 3 | import java.util.PriorityQueue; 4 | 5 | public class Haffman { 6 | PriorityQueue queue1 = new PriorityQueue<>(); 7 | PriorityQueue queue2 = new PriorityQueue<>(); 8 | class Node implements Comparable { 9 | int frec; 10 | char c; 11 | boolean leaf; 12 | Node right,left; 13 | int index; 14 | String chars; 15 | Node(int index,char c,int frec,boolean leaf){ 16 | this.index = index; 17 | this.frec = frec; 18 | this.c = c; 19 | this.leaf = leaf; 20 | }Node(char c,int frec,boolean leaf,Node left,Node right){ 21 | this.frec = frec; 22 | this.c = c; 23 | this.leaf = leaf; 24 | this.left = left; 25 | this.right = right; 26 | } 27 | 28 | public void setChars(String chars) { 29 | this.chars = chars; 30 | } 31 | 32 | @Override 33 | public int compareTo(Node o) { 34 | return this.frec-o.frec; 35 | } 36 | } 37 | public void init(int[] frec, char[] chars){ 38 | for (int i = 0; i void printMat(T mat[][]){ 97 | for (int i = 0; i { 8 | int src; 9 | int dst; 10 | int weight; 11 | public EdgeD(int src,int dst, int weight){ 12 | this.dst = dst; 13 | this.src = src; 14 | this.weight = weight; 15 | } 16 | 17 | @Override 18 | public int compareTo(EdgeD o) { 19 | return this.weight - o.weight; 20 | } 21 | } 22 | public class dijkstra { 23 | public static int inf = Integer.MAX_VALUE; 24 | private HashMap> G = new LinkedHashMap<>(); 25 | 26 | public ArrayList shortestPath(int src,int dst ,HashMap> g ){ 27 | int[] cost = new int[g.size()]; 28 | ArrayList[] path = new ArrayList[g.size()]; 29 | for (int i = 0; i(); 32 | } 33 | cost[src] =0; 34 | path[src].add(src); 35 | if (src==dst) return new ArrayList<>(src); 36 | if (g.get(src)==null||g.get(src).size()==0)return new ArrayList<>(); 37 | PriorityQueue minEq = new PriorityQueue<>(); 38 | for (EdgeD edge:g.get(src).values()){ 39 | minEq.add(edge); 40 | } 41 | boolean finish = false; 42 | while (!finish) { 43 | EdgeD eTemp= minEq.poll(); 44 | int tDst=eTemp.dst,tSrc = eTemp.src,wTemp = eTemp.weight; 45 | if (cost[tDst]==inf||cost[tDst] > cost[tSrc]+wTemp ){ 46 | cost[tDst] = cost[tSrc]+wTemp; 47 | System.out.println(cost[tSrc]); 48 | for (int i: path[tSrc]){ 49 | path[tDst].add(i); 50 | } 51 | path[tDst].add(tDst); 52 | for (EdgeD edge:g.get(tDst).values()){ 53 | minEq.add(edge); 54 | } 55 | } 56 | finish = minEq.isEmpty() || tDst == dst; 57 | } 58 | return path[dst]; 59 | 60 | } 61 | public static void main(String[] args) { 62 | HashMap> g = new LinkedHashMap<>(); 63 | EdgeD e01 = new EdgeD(0,1,3); 64 | EdgeD e02 = new EdgeD(0,2,1); 65 | 66 | EdgeD e10 = new EdgeD(1,0,3); 67 | EdgeD e13 = new EdgeD(1,3,2); 68 | EdgeD e12 = new EdgeD(1,2,1); 69 | 70 | EdgeD e20 = new EdgeD(2,0,1); 71 | EdgeD e21 = new EdgeD(2,1,1); 72 | EdgeD e24 = new EdgeD(2,4,5); 73 | 74 | EdgeD e34 = new EdgeD(3,4,1); 75 | EdgeD e31 = new EdgeD(3,1,2); 76 | EdgeD e35 = new EdgeD(3,5,9); 77 | EdgeD e36 = new EdgeD(3,6,5); 78 | 79 | EdgeD e42 = new EdgeD(4,2,5); 80 | EdgeD e43 = new EdgeD(4,3,1); 81 | EdgeD e46 = new EdgeD(4,6,5); 82 | 83 | EdgeD e53 = new EdgeD(5,3,9); 84 | EdgeD e56 = new EdgeD(5,6,8); 85 | EdgeD e57 = new EdgeD(5,7,2); 86 | 87 | EdgeD e65 = new EdgeD(6,5,8); 88 | EdgeD e63 = new EdgeD(6,3,5); 89 | EdgeD e64 = new EdgeD(6,4,5); 90 | EdgeD e67 = new EdgeD(6,7,1); 91 | 92 | EdgeD e76 = new EdgeD(7,6,1); 93 | EdgeD e75 = new EdgeD(7,5,2); 94 | 95 | 96 | for (int i = 0;i<8;i++){ 97 | g.put(i,new HashMap<>()); 98 | } 99 | g.get(0).put(1,e01); 100 | g.get(0).put(2,e02); 101 | 102 | g.get(1).put(0,e10); 103 | g.get(1).put(2,e12); 104 | g.get(1).put(3,e13); 105 | 106 | g.get(2).put(0,e20); 107 | g.get(2).put(1,e21); 108 | g.get(2).put(4,e24); 109 | 110 | g.get(3).put(1,e31); 111 | g.get(3).put(4,e34); 112 | g.get(3).put(5,e35); 113 | g.get(3).put(6,e36); 114 | 115 | g.get(4).put(2,e42); 116 | g.get(4).put(3,e43); 117 | g.get(4).put(6,e46); 118 | 119 | g.get(5).put(3,e53); 120 | g.get(5).put(6,e56); 121 | g.get(5).put(7,e57); 122 | 123 | g.get(6).put(3,e63); 124 | g.get(6).put(4,e64); 125 | g.get(6).put(5,e65); 126 | g.get(6).put(7,e67); 127 | 128 | g.get(7).put(5,e75); 129 | g.get(7).put(6,e76); 130 | 131 | dijkstra dijkstra = new dijkstra(); 132 | System.out.println(dijkstra.shortestPath(0,5,g)); 133 | } 134 | 135 | } 136 | -------------------------------------------------------------------------------- /src/Kruskal.java: -------------------------------------------------------------------------------- 1 | 2 | import javax.script.Compilable; 3 | import java.util.*; 4 | 5 | class EdgeW implements Comparable { 6 | private int src; 7 | private int dst; 8 | private int weight; 9 | public EdgeW(int src, int dst, int weight) { 10 | this.dst = dst; 11 | this.src = src; 12 | this.weight = weight; 13 | } 14 | 15 | public int getDst() { 16 | return dst; 17 | } 18 | 19 | public int getSrc() { 20 | return src; 21 | } 22 | 23 | public int getWeight() { 24 | return weight; 25 | } 26 | 27 | @Override 28 | public int compareTo(EdgeW o) { 29 | return weight-o.weight; 30 | } 31 | 32 | @Override 33 | public String toString() { 34 | return src+"->"+dst + " w:"+weight; 35 | } 36 | } 37 | 38 | public class Kruskal{ 39 | private HashMap> G = new LinkedHashMap<>(); 40 | public HashMap> 41 | kruskel(HashMap> g) throws Exception { 42 | HashMap> Tree = new LinkedHashMap<>(); 43 | Union[] union = new Union[g.size()]; 44 | for (int i = 0;i listE_sorted = new PriorityQueue<>(); 46 | for (int i = 0; i < g.size(); i++) { 47 | Collection collection = g.get(i).values(); 48 | for (EdgeW edgeW : collection) { 49 | listE_sorted.add(edgeW); 50 | g.get(edgeW.getDst()).remove(i); 51 | } 52 | } 53 | while (Tree.size()()); 62 | Tree.get(src).put(dst,edgeW); 63 | Union.Union(union[src],union[dst]); 64 | } 65 | else { 66 | System.out.println(src+" "+ dst); 67 | } 68 | } 69 | 70 | return Tree; 71 | } 72 | } 73 | class Union { 74 | private Union parent; 75 | private int rank; 76 | public Union(){ 77 | parent = this; 78 | rank = 0; 79 | } 80 | 81 | public static Union find(Union x) { 82 | if (x.parent != x) { 83 | x.parent = find(x.parent); 84 | x = x.parent; 85 | } 86 | return x.parent; 87 | } 88 | public static void Union(Union x, Union y) { 89 | Union xRoot = find(x); 90 | Union yRoot = find(y); 91 | if(xRoot== yRoot) 92 | return; 93 | if(xRoot.rank < yRoot.rank) 94 | xRoot.parent = yRoot; 95 | else if(xRoot.rank > yRoot.rank) 96 | yRoot.parent = xRoot; 97 | else{ 98 | yRoot.parent = xRoot; 99 | xRoot.rank = xRoot.rank + 1; 100 | } 101 | 102 | } 103 | public static void main(String[] args) throws Exception { 104 | EdgeW e0 = new EdgeW(0,1,2); 105 | EdgeW e1 = new EdgeW(1,2,12); 106 | EdgeW e2 = new EdgeW(2,3,2); 107 | EdgeW e3 = new EdgeW(2,4,5); 108 | EdgeW e4 = new EdgeW(3,4,1); 109 | EdgeW e5 = new EdgeW(3,5,3); 110 | EdgeW e6 = new EdgeW(3,1,11); 111 | EdgeW e7 = new EdgeW(2,5,8); 112 | HashMap> g = new LinkedHashMap<>(); 113 | for (int i = 0;i<6;i++){ 114 | g.put(i,new HashMap<>()); 115 | } 116 | g.get(0).put(1,e0); 117 | g.get(1).put(2,e1); 118 | g.get(2).put(3,e2); 119 | g.get(2).put(4,e3); 120 | g.get(3).put(4,e4); 121 | g.get(3).put(5,e5); 122 | g.get(3).put(1,e6); 123 | g.get(2).put(5,e7); 124 | Kruskal k = new Kruskal(); 125 | HashMap> ans = k.kruskel(g); 126 | for (int i = 0; i<6;i++ ){ 127 | if (ans.get(i)!=null) 128 | for (EdgeW e:ans.get(i).values()){ 129 | System.out.print(e+" "); 130 | } 131 | } 132 | } 133 | } 134 | 135 | -------------------------------------------------------------------------------- /src/BucketsProblem.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | public class BucketsProblem { 4 | private int sizeBucketA; 5 | private int sizeBucketB; 6 | private boolean[][] neighborMatrix; 7 | private boolean[][] trackMatrix; 8 | private String[][] theTrac; 9 | public BucketsProblem(int sizeBucketA,int sizeBucketB){ 10 | this.sizeBucketA = sizeBucketA; 11 | this.sizeBucketB = sizeBucketB; 12 | int m = sizeBucketA+1; 13 | int n = sizeBucketB+1; 14 | neighborMatrix = new boolean[n*m][n*m]; 15 | createTable(); 16 | trackMatrix = new boolean[n*m][n*m]; 17 | theTrac = new String[n*m][n*m]; 18 | createTraks(); 19 | } 20 | 21 | private void createTable() { 22 | int m = sizeBucketA; 23 | int n = sizeBucketB; 24 | for (int k = 0; k < neighborMatrix.length; k++){ 25 | int capacityA = k / (n + 1); 26 | int capacityB = k % (n + 1); 27 | neighborMatrix[k][k]=true; 28 | int index = 0; 29 | 30 | index = emptyA(capacityA,capacityB); 31 | neighborMatrix[k][index] = true; 32 | 33 | index = emptyB(capacityA,capacityB); 34 | neighborMatrix[k][index] = true; 35 | 36 | index = fillA(capacityA,capacityB); 37 | neighborMatrix[k][index] = true; 38 | 39 | index = fillB(capacityA,capacityB); 40 | neighborMatrix[k][index] = true; 41 | 42 | index = AtoB(capacityA,capacityB); 43 | neighborMatrix[k][index] = true; 44 | 45 | index = BtoA(capacityA,capacityB); 46 | neighborMatrix[k][index] = true; 47 | } 48 | } 49 | int emptyA(int capacityA,int capacityB){ 50 | return capacityB; 51 | } 52 | int emptyB(int capacityA,int capacityB){ 53 | int n = (sizeBucketB+1); 54 | return capacityA * n; 55 | } 56 | int fillA(int capacityA,int capacityB){ 57 | int n = (sizeBucketB+1); 58 | return n * sizeBucketA + capacityB; 59 | } 60 | int fillB(int capacityA,int capacityB){ 61 | int n = (sizeBucketB+1); 62 | return n * capacityA + sizeBucketB; 63 | } 64 | int AtoB(int capacityA, int capacityB){ 65 | int n = (sizeBucketB+1); 66 | int j = Math.min(capacityB+capacityA,sizeBucketB); 67 | int i = capacityB+capacityA - j; 68 | return n * i + j; 69 | } 70 | int BtoA(int capacityA, int capacityB){ 71 | int n = (sizeBucketB+1); 72 | int i = Math.min(capacityB+capacityA,sizeBucketA); 73 | int j = capacityB+capacityA - i; 74 | return n * i + j; 75 | 76 | } 77 | 78 | private void createTraks() { 79 | for (int i = 0; i < neighborMatrix.length; i++) { 80 | int cA = i / (sizeBucketB + 1); 81 | int cB = i % (sizeBucketB + 1); 82 | String s2 = "(" + cA + " ," + cB + ")"; 83 | for (int j = 0; j < neighborMatrix[i].length; j++) { 84 | int capacityA = j / (sizeBucketB + 1); 85 | int capacityB = j % (sizeBucketB + 1); 86 | String s = s2 + " -> (" + capacityA + " ," + capacityB + ")"; 87 | if (cA == capacityA && cB == capacityB) { 88 | theTrac[i][j] = s2; 89 | } else if (neighborMatrix[i][j]) { 90 | theTrac[i][j] = s + ""; 91 | } 92 | } 93 | } 94 | for (int k = 0; k < neighborMatrix.length; k++) { 95 | for (int i = 0; i < neighborMatrix.length; i++) { 96 | for (int j = 0; j < neighborMatrix[i].length; j++) { 97 | trackMatrix[i][j] = (neighborMatrix[i][j] || (neighborMatrix[i][k] && neighborMatrix[k][j])) || (trackMatrix[i][j] || (trackMatrix[i][k] && trackMatrix[k][j])); 98 | } 99 | } 100 | } 101 | int go = 1; 102 | boolean[][] fill = new boolean[neighborMatrix.length][neighborMatrix.length]; 103 | while (go>0) { 104 | for (int k = 0; k < neighborMatrix.length; k++) { 105 | for (int i = 0; i < neighborMatrix.length; i++) { 106 | for (int j = 0; j < neighborMatrix[i].length; j++) { 107 | if (!neighborMatrix[i][j] && trackMatrix[i][j] && (theTrac[i][j] == null || i == j)) { 108 | if (theTrac[i][k] != null && theTrac[k][j] != null && !fill[i][j]) { 109 | fill[i][j] = true; 110 | theTrac[i][j] = theTrac[i][k] + " -> " + theTrac[k][j]; 111 | } 112 | else go++; 113 | } 114 | 115 | 116 | } 117 | } 118 | } 119 | go--; 120 | } 121 | 122 | } 123 | 124 | 125 | 126 | public void printM(boolean[][] arr){ 127 | String[] arrayS = new String[arr.length]; 128 | for (int i = 0; i queue = new LinkedList<>(); 37 | queue.add(id); 38 | color[id] = Color.GRAY; 39 | d[id] = 0; 40 | queue.add(id); 41 | while (!queue.isEmpty()){ 42 | int v = queue.poll(); 43 | Collection temp = graph.getEdges(v); 44 | if (temp != null) 45 | for (edge_data e: temp){ 46 | int u = e.getDst(); 47 | if (color[u] == Color.WHITE){ 48 | color[u] = Color.GRAY; 49 | d[u] = d[v] + 1; 50 | pi[u] = v; 51 | queue.add(u); 52 | } 53 | } 54 | color[v] = Color.BLACK; 55 | } 56 | 57 | } 58 | public boolean connected(){ 59 | bfs(1); 60 | int sum = 0; 61 | for (int i:pi){ 62 | if (i == -1) 63 | sum++; 64 | } 65 | return sum == 2;//include in place 0 66 | } 67 | private void reBfsNoReinit(int id) { 68 | Queue queue = new LinkedList<>(); 69 | queue.add(id); 70 | color[id] = Color.GRAY; 71 | d[id] = 0; 72 | queue.add(id); 73 | while (!queue.isEmpty()) { 74 | int v = queue.poll(); 75 | Collection temp = graph.getEdges(v); 76 | if (temp != null) 77 | for (edge_data e : temp) { 78 | int u = e.getDst(); 79 | if (color[u] == Color.WHITE) { 80 | color[u] = Color.GRAY; 81 | d[u] = d[v] + 1; 82 | pi[u] = v; 83 | queue.add(u); 84 | } 85 | } 86 | color[v] = Color.BLACK; 87 | } 88 | } 89 | private void fillTable(int[] what, int sine){ 90 | for (int i = 1; i more than one 99 | int sum = 1; 100 | int[] what = new int[size]; 101 | fillTable(what,sum); 102 | for (int i = 1; i < color.length; i++){ 103 | if (color[i] == Color.WHITE){ 104 | sum ++; 105 | reBfsNoReinit(i); 106 | fillTable(what,sum); 107 | } 108 | } 109 | System.out.println(Arrays.toString(what)); 110 | return sum; 111 | } 112 | private int getMaxD(boolean maxORindex){ 113 | int max = -1; 114 | int index = -1; 115 | for (int i = 1;i < d.length;i++){ 116 | if (d[i] > max) { 117 | max = d[i]; 118 | index = i; 119 | } 120 | } 121 | if (maxORindex) 122 | return max; 123 | else return index; 124 | } 125 | public int findDiam(){ //O(|V| * |V+E|) 126 | int max = -1; 127 | for (int i = 1; i path(int src,int dst){ 142 | bfs(src); 143 | if (d[dst] == -1) { 144 | System.out.println("no path"); 145 | return null; 146 | } 147 | System.out.println("the distance between " + src+" to " + dst + " is = "+d[dst]); 148 | List path = new LinkedList<>(); 149 | int current = dst; 150 | while (current != -1){ 151 | path.add(current); 152 | current = pi[current]; 153 | } 154 | return path; 155 | 156 | 157 | } 158 | private int findHead() { 159 | for (int i=1; i