diff --git a/1-Fundamentals/1-1-BasicProgModel/Ex_1_1_14.java b/1-Fundamentals/1-1-BasicProgModel/Ex_1_1_14.java new file mode 100644 index 0000000..081c115 --- /dev/null +++ b/1-Fundamentals/1-1-BasicProgModel/Ex_1_1_14.java @@ -0,0 +1,19 @@ +public class Ex_1_1_14 { + public static void main(String[] args){ + System.out.println("Hello World"); + System.out.println(lg(1)); + System.out.println(lg(2)); + System.out.println(lg(40)); + System.out.println(lg(64)); + } + + public static int lg(int val){ + int sum = 0; + while(val>=2) { + val /= 2; + sum++; + } + return sum; + } + +} \ No newline at end of file diff --git a/4-Graphs/4-2-DirectedGraphs/Ex_4_2_10.java b/4-Graphs/4-2-DirectedGraphs/Ex_4_2_10.java new file mode 100644 index 0000000..1c442df --- /dev/null +++ b/4-Graphs/4-2-DirectedGraphs/Ex_4_2_10.java @@ -0,0 +1,27 @@ + + +// Topological sort: Given a digraph, give a permutation of the vertices such that if v->w for all +// the edges then in that permutation, w never comes before v. + +// Lets say the graph is edges of pre-reqs, then if you take courses in the topological order, +// It will never occur in the list that a course has a pre-req and it is found later in the list. + +public class Ex_4_2_10 { + +/** + * Given a DAG, does there exist a topological order that cannot result from applying + * a DFS-based algorithm, no matter in what order the vertices adjacent to each + * vertex are chosen? Prove your answer. + * @author VINCE + * + */ + +//Yes that can be such a case. +//1->2 +//1->3 +//3->4 +// +//Topological sort order: [1 3 2 4] +//Can never be achieved through DFS. + +} diff --git a/4-Graphs/4-2-DirectedGraphs/Ex_4_2_12.java b/4-Graphs/4-2-DirectedGraphs/Ex_4_2_12.java new file mode 100644 index 0000000..b03cab0 --- /dev/null +++ b/4-Graphs/4-2-DirectedGraphs/Ex_4_2_12.java @@ -0,0 +1,13 @@ + + + +public class Ex_4_2_12 { + /** How many edges are there in the transitive closure of a digraph that is a simple + * directed path with V vertices and V–1 edges? + */ + +// In a line graph if there are v vertices then there are v-1 edges. +// Total number of edges for transitive graph of a line graph is V-1{original} + (v-2)+(v-3)+,,,,, +// So the total number is v*(v-1)/2 + +} diff --git a/4-Graphs/4-2-DirectedGraphs/Ex_4_2_9.java b/4-Graphs/4-2-DirectedGraphs/Ex_4_2_9.java new file mode 100644 index 0000000..b9331e5 --- /dev/null +++ b/4-Graphs/4-2-DirectedGraphs/Ex_4_2_9.java @@ -0,0 +1,67 @@ + + + + + + +public class Ex_4_2_9 { + + /** + * Write a method that checks whether or not a given permutation of a DAG’s vertices + * is a topological order of that DAG. + */ + + + public static void main(String[] args) { + + } + + /** + * + * @param G = Graph + * @param order = topological sort order + * @return whether the topological order is correct for the given graph G + * + * This method does its Job in O(E.V) time. + */ + public static boolean isValidTopologicalSort(Graph G,int[] order){ + // The approach will be check for each edge v->w, v should come before w in order[]. + // I can think of O(n2) method for it. + // Take the all the edges from G.adj() method and then check in order[] + + // http://cstheory.stackexchange.com/questions/4414/testing-identifying-a-topological-sorting + // All the edges need to be considered. Ω(E) is the lowerbound. + + boolean ans = true; + for(int v = 0; v < G.V();v++){ + for(int w : G.adj(v)){ + ans = ans && isLaterinArray(v, w, order); + } + } + return ans; + } + + private static boolean isLaterinArray(int first,int second,int[] array){ + int indexFirst = -1; + int indexSecond = -1; + for (int i = 0; i < array.length; i++) { + if(array[i]==first){ + indexFirst = i; + } + if(array[i]==second){ + indexSecond = i; + } + if(indexFirst!=-1 && indexSecond!=-1){ + break; + } + } + + if(indexSecond>indexFirst){ + return true; + } + else{ + return false; + } + } + +}