-
Notifications
You must be signed in to change notification settings - Fork 307
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Create Kosaraju_algorithm The Kosaraju algorithm is an important graph algorithm to detect strongly connected components in a graph. A directed graph is strongly connected if there is a path between all pairs of vertices. A strongly connected component (SCC) of a directed graph is a maximal strongly connected subgraph. This algorithm is often the first step in solving many graph questions. * Create Dynamic_fibonacci.py
- Loading branch information
1 parent
cde815b
commit 1ea0d04
Showing
2 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Function for nth fibonacci number - Dynamic Programing | ||
# Taking 1st two fibonacci nubers as 0 and 1 | ||
|
||
FibArray = [0,1] | ||
|
||
def fibonacci(n): | ||
if n<0: | ||
print("Incorrect input") | ||
elif n<=len(FibArray): | ||
return FibArray[n-1] | ||
else: | ||
temp_fib = fibonacci(n-1)+fibonacci(n-2) | ||
FibArray.append(temp_fib) | ||
return temp_fib | ||
|
||
# Driver Program | ||
#Suppose we wan't to print the 9th fibonacci number | ||
#You can ask for user input and substitute in place of 9 | ||
|
||
print(fibonacci(9)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
#include <iostream> | ||
#include <list> | ||
#include <stack> | ||
using namespace std; | ||
|
||
class Graph | ||
{ | ||
int V; // No. of vertices | ||
list<int> *adj; // An array of adjacency lists | ||
|
||
// Fills Stack with vertices (in increasing order of finishing | ||
// times). The top element of stack has the maximum finishing | ||
// time | ||
void fillOrder(int v, bool visited[], stack<int> &Stack); | ||
|
||
// A recursive function to print DFS starting from v | ||
void DFSUtil(int v, bool visited[]); | ||
public: | ||
Graph(int V); | ||
void addEdge(int v, int w); | ||
|
||
// The main function that finds and prints strongly connected | ||
// components | ||
void printSCCs(); | ||
|
||
// Function that returns reverse (or transpose) of this graph | ||
Graph getTranspose(); | ||
}; | ||
|
||
Graph::Graph(int V) | ||
{ | ||
this->V = V; | ||
adj = new list<int>[V]; | ||
} | ||
|
||
// A recursive function to print DFS starting from v | ||
void Graph::DFSUtil(int v, bool visited[]) | ||
{ | ||
// Mark the current node as visited and print it | ||
visited[v] = true; | ||
cout << v << " "; | ||
|
||
// Recur for all the vertices adjacent to this vertex | ||
list<int>::iterator i; | ||
for (i = adj[v].begin(); i != adj[v].end(); ++i) | ||
if (!visited[*i]) | ||
DFSUtil(*i, visited); | ||
} | ||
|
||
Graph Graph::getTranspose() | ||
{ | ||
Graph g(V); | ||
for (int v = 0; v < V; v++) | ||
{ | ||
// Recur for all the vertices adjacent to this vertex | ||
list<int>::iterator i; | ||
for(i = adj[v].begin(); i != adj[v].end(); ++i) | ||
{ | ||
g.adj[*i].push_back(v); | ||
} | ||
} | ||
return g; | ||
} | ||
|
||
void Graph::addEdge(int v, int w) | ||
{ | ||
adj[v].push_back(w); // Add w to v’s list. | ||
} | ||
|
||
void Graph::fillOrder(int v, bool visited[], stack<int> &Stack) | ||
{ | ||
// Mark the current node as visited and print it | ||
visited[v] = true; | ||
|
||
// Recur for all the vertices adjacent to this vertex | ||
list<int>::iterator i; | ||
for(i = adj[v].begin(); i != adj[v].end(); ++i) | ||
if(!visited[*i]) | ||
fillOrder(*i, visited, Stack); | ||
|
||
// All vertices reachable from v are processed by now, push v | ||
Stack.push(v); | ||
} | ||
|
||
// The main function that finds and prints all strongly connected | ||
// components | ||
void Graph::printSCCs() | ||
{ | ||
stack<int> Stack; | ||
|
||
// Mark all the vertices as not visited (For first DFS) | ||
bool *visited = new bool[V]; | ||
for(int i = 0; i < V; i++) | ||
visited[i] = false; | ||
|
||
// Fill vertices in stack according to their finishing times | ||
for(int i = 0; i < V; i++) | ||
if(visited[i] == false) | ||
fillOrder(i, visited, Stack); | ||
|
||
// Create a reversed graph | ||
Graph gr = getTranspose(); | ||
|
||
// Mark all the vertices as not visited (For second DFS) | ||
for(int i = 0; i < V; i++) | ||
visited[i] = false; | ||
|
||
// Now process all vertices in order defined by Stack | ||
while (Stack.empty() == false) | ||
{ | ||
// Pop a vertex from stack | ||
int v = Stack.top(); | ||
Stack.pop(); | ||
|
||
// Print Strongly connected component of the popped vertex | ||
if (visited[v] == false) | ||
{ | ||
gr.DFSUtil(v, visited); | ||
cout << endl; | ||
} | ||
} | ||
} | ||
|
||
// Driver program to test above functions | ||
int main() | ||
{ | ||
// Create a graph given in the above diagram | ||
int n ; | ||
cout << "Enter how many vertices your graph will have?" << endl ; | ||
cin >> n ; | ||
|
||
Graph g(n) ; | ||
char ch = 'y' ; | ||
|
||
while(ch=='y'){ | ||
int src ; | ||
int dst ; | ||
cout << "Enter the originating vertice and the destination vertice with a space" << endl ; | ||
cin >> src >> dst ; | ||
|
||
g.addEdge(src, dst) ; | ||
|
||
cout << "Do you want to add another edge? type 'y' for yes, 'n' for no" << endl ; | ||
cin >> ch ; | ||
cout << endl ; | ||
} | ||
|
||
|
||
cout << "Following are strongly connected components in " | ||
"given graph \n"; | ||
g.printSCCs(); | ||
|
||
return 0; | ||
} |