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
19 changes: 19 additions & 0 deletions 1-Fundamentals/1-1-BasicProgModel/Ex_1_1_14.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
27 changes: 27 additions & 0 deletions 4-Graphs/4-2-DirectedGraphs/Ex_4_2_10.java
Original file line number Diff line number Diff line change
@@ -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.

}
13 changes: 13 additions & 0 deletions 4-Graphs/4-2-DirectedGraphs/Ex_4_2_12.java
Original file line number Diff line number Diff line change
@@ -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

}
67 changes: 67 additions & 0 deletions 4-Graphs/4-2-DirectedGraphs/Ex_4_2_9.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

}