Skip to content

Commit 39db948

Browse files
committed
2 parents 0bd84ef + 4893c26 commit 39db948

File tree

8 files changed

+452
-28
lines changed

8 files changed

+452
-28
lines changed

Data Structures/Graphs/Khan_algo.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
'''
2+
author :saurabh jain
3+
to find whether a graph has a cycle or not not
4+
if not than print the topological sorting for them
5+
'''
6+
from collections import defaultdict
7+
8+
9+
class solution:
10+
11+
def __init__(self, vrt):
12+
self.vrt = vrt
13+
self.graph = defaultdict(list)
14+
15+
def edges(self, u, v):
16+
global vertex,indegree
17+
self.graph[u].append(v)
18+
if u not in indegree.keys():
19+
indegree[u]=0
20+
if v not in indegree.keys():
21+
indegree[v]=1
22+
23+
else:
24+
indegree[v]+=1
25+
vertex.add(u)
26+
vertex.add(v)
27+
28+
def topological(self):
29+
global vertex,indegree
30+
q=[]
31+
count=0
32+
for i in vertex:
33+
if indegree[i]==0:
34+
q.append(i)
35+
36+
result=[]
37+
38+
while q:
39+
40+
current=q.pop(0)
41+
count+=1
42+
result.append(current)
43+
44+
45+
46+
for i in self.graph[current]:
47+
indegree[i]-=1
48+
if indegree[i]==0:
49+
q.append(i)
50+
51+
if count!=len(vertex):
52+
print("there is no topolical string ")
53+
else:
54+
print(result)
55+
56+
57+
58+
return
59+
60+
61+
62+
63+
64+
indegree=defaultdict()
65+
vertex=set()
66+
67+
g =solution(6)
68+
g.edges(5, 2);
69+
g.edges(5, 0);
70+
g.edges(4, 0);
71+
g.edges(4, 1);
72+
g.edges(2, 3);
73+
g.edges(3, 1)
74+
print(indegree)
75+
print(vertex)
76+
77+
g.topological()
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
Topological sorting for Directed Acyclic Graph (DAG) is a linear ordering
3+
of vertices such that for every directed edge uv, vertex u comes before v in
4+
the ordering. Topological Sorting for a graph is not possible if the graph is
5+
not a DAG.
6+
*/
7+
8+
#include <bits/stdc++.h>
9+
using namespace std;
10+
11+
vector<vector<int>> adjList;
12+
stack<int> s;
13+
bool *visited;
14+
15+
// Number of Test Cases (Vertices)
16+
int n;
17+
18+
// function to Create and represent a graph by storing data.
19+
void createGraph()
20+
{
21+
int x, y;
22+
adjList.resize(n);
23+
24+
for (int j = 0; j < n; j++)
25+
{
26+
cin >> x >> y;
27+
adjList[x].push_back(y);
28+
}
29+
}
30+
31+
// A recursive function used by topoSortFun.
32+
void topoSortFun(int i)
33+
{
34+
visited[i] = true;
35+
for (int j = 0; j < adjList[i].size(); j++)
36+
{
37+
int key = adjList[i][j];
38+
if (!visited[key])
39+
topoSortFun(key);
40+
}
41+
s.push(i);
42+
}
43+
44+
// The function to do Topological Sort. It uses recursive topoSortFun().
45+
void topoSort()
46+
{
47+
memset(visited, false, sizeof(visited));
48+
49+
for (int i = 0; i < n; i++)
50+
{
51+
if (!visited[i])
52+
topoSortFun(i);
53+
}
54+
}
55+
56+
void display()
57+
{
58+
for (int i = 0; i < n; i++)
59+
{
60+
cout << s.top() << " ";
61+
s.pop();
62+
}
63+
}
64+
65+
int main()
66+
{
67+
cout<<"Enter number of node you want to add: ";
68+
cin >> n;
69+
visited = new bool[n];
70+
cout<<"Enter nodes of graph: ";
71+
createGraph();
72+
topoSort();
73+
cout<<"Graph after topological sort: \n";
74+
display();
75+
}
76+
77+
/*
78+
Test Case:
79+
Input:
80+
Enter number of node you want to add: 6
81+
Enter nodes of graph:
82+
5 2
83+
5 0
84+
4 0
85+
4 1
86+
2 3
87+
3 1
88+
Output:
89+
Graph after topological sort:
90+
5 4 2 3 1 0
91+
92+
Time Complexity: O(V + E) – where V is the number of vertices and E is the number of edges.
93+
Space Complexity: O(V)
94+
*/

Data Structures/Graphs/WordBoggle.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,40 @@
1+
// Given a dictionary of distinct words and an M x N board where every cell has one character. Find all possible words from the dictionary that can be formed by a sequence of adjacent characters on the board. We can move to any of 8 adjacent characters, but a word should not have multiple instances of the same cell.
2+
// Example 1:
3+
4+
// Input:
5+
// N = 1
6+
// dictionary = {"CAT"}
7+
// R = 3, C = 3
8+
// board = {{C,A,P},{A,N,D},{T,I,E}}
9+
// Output:
10+
// CAT
11+
// Explanation:
12+
// C A P
13+
// A N D
14+
// T I E
15+
// Words we got is denoted using same color.
16+
// Example 2:
17+
18+
// Input:
19+
// N = 4
20+
// dictionary = {"GEEKS","FOR","QUIZ","GO"}
21+
// R = 3, C = 3
22+
// board = {{G,I,Z},{U,E,K},{Q,S,E}}
23+
// Output:
24+
// GEEKS QUIZ
25+
// Explanation:
26+
// G I Z
27+
// U E K
28+
// Q S E
29+
// Words we got is denoted using same color.
30+
31+
// Companies
32+
// Amazon Directi Facebook Google MakeMyTrip Microsoft Nvidia Yahoo
33+
34+
// Expected Time Complexity: O(NW + RC^2)
35+
// Expected Auxiliary Space: O(NW + RC)
36+
------------------------------------------------------------
37+
138
#include<bits/stdc++.h>
239
using namespace std;
340

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
A Linked List is a linear data structure which includes a series of connected nodes.
3+
Each node stores the data and the address of the next node.
4+
If any node is visited more than once while traversing the list then we can say that it contains cycle/loop.
5+
Example:
6+
1 -> 2 -> 3
7+
^ |
8+
| v
9+
5 <- 4
10+
Output:
11+
True
12+
Method:In this method we will traverse the linkedlist using two pointers is slow and fast.
13+
Move the slow pointer by one position and fast pointer by two positions.
14+
If these two pointers meet together at any point at same node then we can say that there is loop/cycle inside linkedlist.
15+
*/
16+
17+
#include <bits/stdc++.h>
18+
using namespace std;
19+
20+
// Link list node
21+
class Node
22+
{
23+
public:
24+
int data;
25+
Node *next;
26+
};
27+
28+
void push_node(Node **head_ref, int new_data)
29+
{
30+
/* allocate node */
31+
Node *new_node = new Node();
32+
33+
/* put in the data */
34+
new_node->data = new_data;
35+
36+
/* link the old list off the new node */
37+
new_node->next = (*head_ref);
38+
39+
/* move the head to point to the new node */
40+
(*head_ref) = new_node;
41+
}
42+
43+
int detectLoop(Node *list)
44+
{
45+
Node *slow_p = list, *fast_p = list;
46+
47+
while (slow_p && fast_p && fast_p->next)
48+
{
49+
slow_p = slow_p->next;
50+
fast_p = fast_p->next->next;
51+
if (slow_p == fast_p)
52+
{
53+
return 1;
54+
}
55+
}
56+
return 0;
57+
}
58+
59+
/* Driver code*/
60+
int main()
61+
{
62+
/* Start with the empty list */
63+
Node *head = NULL;
64+
int n = 0;
65+
cout << "Enter size of linked list" << endl;
66+
cin >> n;
67+
int i;
68+
int d;
69+
cout << "Enter the elements of linked list" << endl;
70+
for (int i = 0; i < n; i++)
71+
{
72+
cin >> d;
73+
push_node(&head, d);
74+
}
75+
76+
/* Create a loop for testing */
77+
head->next->next->next->next = head;
78+
if (detectLoop(head))
79+
cout << "True";
80+
else
81+
cout << "False";
82+
return 0;
83+
}
84+
85+
/*
86+
Time complexity: O(n)
87+
Space complexity: O(1)
88+
89+
Input :
90+
Enter size of linked list
91+
5
92+
Enter the elements of linked list
93+
45 24 32 72 54
94+
Output :
95+
True
96+
*/

Data Structures/Trees/Construct_Binary_Tree_from_Preorder_and_Inorder_Traversal.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
// Leetcode problem for Binary Tree
2+
// 105. Construct Binary Tree from Preorder and Inorder Traversal
3+
4+
// Given two integer arrays preorder and inorder where preorder is the preorder traversal of a binary tree and inorder is the inorder traversal of the same tree, construct and return the binary tree.
5+
6+
// Example 1:
7+
8+
// Input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
9+
// Output: [3,9,20,null,null,15,7]
10+
// Example 2:
11+
12+
// Input: preorder = [-1], inorder = [-1]
13+
// Output: [-1]
14+
----------------------------------------------------------------------------
115
/**
216
* Definition for a binary tree node.
317
* struct TreeNode {

README.md

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -47,37 +47,15 @@ Head over to [Contributing Guidelines](https://github.com/smv1999/CompetitivePro
4747

4848
Thanks goes to these **Wonderful People** 👨🏻‍💻:
4949

50-
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
51-
<!-- prettier-ignore-start -->
52-
<!-- markdownlint-disable -->
53-
<table>
54-
<tr>
55-
<td align="center"><a href="https://github.com/smv1999"><img src="https://avatars.githubusercontent.com/u/42896577?s=400&u=9530610016fa2171d559af8bcdb3e9178bb7d308&v=4" width="100px;" alt=""/><br /><sub><b>smv1999</b></sub></a><br /><a href="https://github.com/smv1999/CompetitiveProgrammingQuestionBank/commits?author=smv1999" title="Code">💻</a></td>
56-
<td align="center"><a href="https://github.com/TawfikYasser"><img src="https://avatars.githubusercontent.com/u/54971231?s=400&u=0666d4ced1599a86cdb8d5bb817080ab2cbe22a0&v=4" width="100px;" alt=""/><br /><sub><b>TawfikYasser</b></sub></a><br /><a href="https://github.com/smv1999/CompetitiveProgrammingQuestionBank/commits?author=TawfikYasser" title="Code">💻</a></td>
57-
<td align="center"><a href="https://github.com/Ayush7614"><img src="https://avatars.githubusercontent.com/u/67006255?s=400&u=c0e16c3bba31328a028cfcca4b1fa7599509f905&v=4" width="100px;" alt=""/><br /><sub><b>Ayush7614</b></sub></a><br /><a href="https://github.com/smv1999/CompetitiveProgrammingQuestionBank/commits?author=Ayush7614" title="Code">💻</a></td>
58-
<td align="center"><a href="https://github.com/kiruba-r11"><img src="https://avatars.githubusercontent.com/u/76843281?s=400&u=e505d92cafc37670d23a8b51eb7d99777c46a84e&v=4" width="100px;" alt=""/><br /><sub><b>kiruba-r11</b></sub></a><br /><a href="https://github.com/smv1999/CompetitiveProgrammingQuestionBank/commits?author=kiruba-r11" title="Code">💻</a></td>
59-
<td align="center"><a href="https://github.com/nikita-jain-01"><img src="https://avatars.githubusercontent.com/u/72670446?s=400&u=608b2cb6bb50668db257a6d2a0c9138b53f5eb92&v=4" width="100px;" alt=""/><br /><sub><b>nikita-jain-01</b></sub></a><br /><a href="https://github.com/smv1999/CompetitiveProgrammingQuestionBank/commits?author=nikita-jain-01" title="Code">💻</a></td>
60-
<td align="center"><a href="https://github.com/Rishabh062"><img src="https://avatars.githubusercontent.com/u/57454462?s=400&u=00d039afe29ffad87e32135bc704a6c19aba9784&v=4" width="100px;" alt=""/><br /><sub><b>Rishabh062</b></sub></a><br /><a href="https://github.com/smv1999/CompetitiveProgrammingQuestionBank/commits?author=Rishabh062" title="Code">💻</a></td>
61-
<td align="center"><a href="https://github.com/SarthakKeshari"><img src="https://avatars.githubusercontent.com/u/55992140?v=4" width="100px;" alt=""/><br /><sub><b>SarthakKeshari</b></sub></a><br /><a href="https://github.com/smv1999/CompetitiveProgrammingQuestionBank/commits?author=SarthakKeshari" title="Code">💻</a></td>
62-
</tr>
63-
<tr>
64-
<td align="center"><a href="https://github.com/MannyP31"><img src="https://avatars.githubusercontent.com/u/76945004?v=4" width="100px;" alt=""/><br /><sub><b>MannyP31</b></sub></a><br /><a href="https://github.com/smv1999/CompetitiveProgrammingQuestionBank/commits?author=MannyP31" title="Code">💻</a></td>
65-
<td align="center"><a href="https://github.com/dsrao711"><img src="https://avatars.githubusercontent.com/u/59830064?v=4" width="100px;" alt=""/><br /><sub><b>dsrao711</b></sub></a><br /><a href="https://github.com/smv1999/CompetitiveProgrammingQuestionBank/commits?author=dsrao711" title="Code">💻</a></td>
66-
<td align="center"><a href="https://github.com/UG-SEP"><img src="https://avatars.githubusercontent.com/u/75884061?v=4" width="100px;" alt=""/><br /><sub><b>UG-SEP</b></sub></a><br /><a href="https://github.com/smv1999/CompetitiveProgrammingQuestionBank/commits?author=UG-SEP" title="Code">💻</a></td>
67-
<td align="center"><a href="https://github.com/gaurav24072002"><img src="https://avatars.githubusercontent.com/u/77109758?v=4" width="100px;" alt=""/><br /><sub><b>gaurav24072002</b></sub></a><br /><a href="https://github.com/smv1999/CompetitiveProgrammingQuestionBank/commits?author=gaurav24072002" title="Code">💻</a></td>
68-
<td align="center"><a href="https://github.com/version0chiro"><img src="https://avatars.githubusercontent.com/u/56084650?v=4" width="100px;" alt=""/><br /><sub><b>version0chiro</b></sub></a><br /><a href="https://github.com/smv1999/CompetitiveProgrammingQuestionBank/commits?author=version0chiro" title="Code">💻</a></td>
69-
<td align="center"><a href="https://github.com/Abhishek-072"><img src="https://avatars.githubusercontent.com/u/79994128?v=4" width="100px;" alt=""/><br /><sub><b>Abhishek-072</b></sub></a><br /><a href="https://github.com/smv1999/CompetitiveProgrammingQuestionBank/commits?author=Abhishek-072" title="Code">💻</a></td>
70-
<td align="center"><a href="https://github.com/jahnvisrivastava100"><img src="https://avatars.githubusercontent.com/u/60891187?v=4" width="100px;" alt=""/><br /><sub><b>jahnvisrivastava100</b></sub></a><br /><a href="https://github.com/smv1999/CompetitiveProgrammingQuestionBank/commits?author=jahnvisrivastava100" title="Code">💻</a></td>
71-
</tr>
72-
<tr>
73-
<td align="center"><a href="https://github.com/ManavArora26"><img src="https://avatars.githubusercontent.com/u/72567005?v=4" width="100px;" alt=""/><br /><sub><b>ManavArora26</b></sub></a><br /><a href="https://github.com/smv1999/CompetitiveProgrammingQuestionBank/commits?author=ManavArora26" title="Code">💻</a></td>
74-
<td align="center"><a href="https://github.com/Iamtripathisatyam"><img src="https://avatars.githubusercontent.com/u/69134468?v=4" width="100px;" alt=""/><br /><sub><b>Iamtripathisatyam</b></sub></a><br /><a href="https://github.com/smv1999/CompetitiveProgrammingQuestionBank/commits?author=Iamtripathisatyam" title="Code">💻</a></td>
75-
<td align="center"><a href="https://github.com/pri1311"><img src="https://avatars.githubusercontent.com/u/64613009?v=4" width="100px;" alt=""/><br /><sub><b>pri1311</b></sub></a><br /><a href="https://github.com/smv1999/CompetitiveProgrammingQuestionBank/commits?author=pri1311" title="Code">💻</a></td>
76-
</tr>
77-
</table>
50+
<a href="https://github.com/smv1999/CompetitiveProgrammingQuestionBank/graphs/contributors">
51+
<img src="https://contrib.rocks/image?repo=smv1999/CompetitiveProgrammingQuestionBank" />
52+
</a>
53+
7854

7955
🚀 **Contributions** of any kind is welcome!
8056

57+
58+
8159
## Programs
8260
<img src="https://raw.githubusercontent.com/smv1999/CompetitiveProgrammingQuestionBank/master/images/devincept.gif" alt="DevIncept" />
8361

0 commit comments

Comments
 (0)