Skip to content

Commit 2ae2652

Browse files
authored
Merge branch 'smv1999:master' into master
2 parents e3a6eec + f8326e7 commit 2ae2652

29 files changed

+1949
-0
lines changed

Arrays/max_and_min_element.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Problem Description:
2+
// Here, we have an array arr[]. The array contains n integer values.
3+
// We have to find the maximum value and minimum value out of all values of the array.
4+
5+
// Let’s take an example to understand the problem
6+
7+
// Input : n = 5
8+
// a[n] = { 10, 20, 30, 40, 50}
9+
10+
// Output : Maximum element of array is 50
11+
// Minimum element of array is 10
12+
13+
// Explanation :
14+
//
15+
// There can be multiple solution for this problem.
16+
// One solution is to directly compare the elements of array. This can be done by using two different approaches :
17+
// 1.Iterative approach
18+
// 2.Recursive approach
19+
20+
// Here we are using iterative approach to solve this problem.
21+
// initially we will set a[0] as min/max. Then we iterate over each elemnts using loop and compare it with max/min element of array.
22+
23+
// Time Complexity : O(n)
24+
// Space Complexity : O(1)
25+
26+
#include <iostream>
27+
using namespace std;
28+
29+
int getmax( int a[], int n){
30+
int max = a[0];
31+
for( int i=1; i<n; i++)
32+
{
33+
if( a[i] > max )
34+
max = a[i];
35+
}
36+
37+
return max;
38+
}
39+
40+
int getmin( int a[], int n){
41+
int min = a[0];
42+
for( int i=1; i<n; i++)
43+
{
44+
if( a[i] < min )
45+
min = a[i];
46+
}
47+
48+
return min;
49+
}
50+
51+
int main()
52+
{
53+
int n;
54+
cin>>n;
55+
int a[n];
56+
for( int i=0; i<n; i++)
57+
{
58+
cin>>a[i];
59+
}
60+
int max = getmax( a, n);
61+
int min = getmin( a, n);
62+
cout<<"Maximum element of array is :"<<max<<endl;
63+
cout<<"Minimum element of array is :"<<min<<endl;
64+
65+
}
66+
67+

Backtracking/Hamiltonian_Cycle.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
'''
2+
Hamiltonian Path: a Hamiltonian path (or traceable path) is a path
3+
in an undirected or directed graph that visits each vertex exactly
4+
once. A Hamiltonian cycle (or Hamiltonian circuit) is a Hamiltonian
5+
path that is a cycle.
6+
Aim: To determine the existance of a Hamiltonian Cycle in the provided
7+
undirected graph and return the path if such path exist.
8+
The nodes are numbered from 1 to N.
9+
10+
'''
11+
12+
from collections import defaultdict
13+
14+
15+
def Move_next_node(n, graph, pos, ans):
16+
# Base Case: If all the node is visited:
17+
if pos == n:
18+
19+
# Check wether the last node and the first node are
20+
# connected in order to form a Cycle
21+
if ans[0] in graph[ans[-1]]:
22+
return ans + [ans[0]]
23+
24+
return False
25+
26+
current = ans[-1]
27+
28+
# Check for each of the adjoining vertices
29+
for i in graph[current]:
30+
31+
# Check wether the node is already visited or not
32+
if i not in ans:
33+
ans += [i]
34+
temp = Move_next_node(n, graph, pos + 1, ans)
35+
if temp:
36+
return temp
37+
38+
ans.pop()
39+
40+
return False
41+
42+
43+
def Hamiltonial_Cycle(n, graph):
44+
# To keep a track of already visited node and the answer
45+
# We will start exploring the graph from Vertex 1
46+
answer = [1]
47+
48+
# Start Exploring adjoining node/vertex
49+
return Move_next_node(n, graph, 1, answer)
50+
51+
52+
# ------------------------DRIVER CODE ------------------------
53+
if __name__ == "__main__":
54+
n, m = map(int, input("Enter the number of vertex and edges: ").split())
55+
print("Enter the edges: ")
56+
graph = defaultdict(list)
57+
for i in range(m):
58+
a, b = map(int, input().split())
59+
graph[a] += [b]
60+
graph[b] += [a]
61+
ans = Hamiltonial_Cycle(n, graph)
62+
if not ans:
63+
print("Hamiltonian Cycle is not possible in the Given graph")
64+
else:
65+
print("Hamiltonian Cycle is possible in the graph")
66+
print("The path is : ", *ans)
67+
68+
'''
69+
70+
Sample Working:
71+
72+
5----1------2
73+
/\ \ /
74+
/ \ \ /
75+
/ \ \/
76+
3------6-----4
77+
Enter the number of vertex and edges: 6 8
78+
Enter the edges
79+
5 1
80+
1 2
81+
1 4
82+
5 6
83+
6 4
84+
4 2
85+
3 6
86+
5 3
87+
Hamiltonian Cycle is possible in the graph
88+
The path is : 1 5 3 6 4 2 1
89+
5----1------2
90+
\ \
91+
\ \
92+
\ \
93+
3------6-----4
94+
Enter the number of vertex and edges: 6 6
95+
Enter the edges:
96+
5 1
97+
3 6
98+
6 4
99+
1 2
100+
5 6
101+
1 4
102+
Hamiltonian Cycle is not possible in the Given graph
103+
104+
COMPLEXITY:
105+
106+
Time Complexity -> O(2^N)
107+
Space Complexity -> O(N)
108+
109+
'''
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
'''
2+
A program for warshall algorithm.It is a shortest path algorithm which is used to find the
3+
distance from source node,which is the first node,to all the other nodes.
4+
If there is no direct distance between two vertices then it is considered as -1
5+
'''
6+
7+
8+
def warshall(g,ver):
9+
dist = list(map(lambda i: list(map(lambda j: j, i)), g))
10+
for i in range(0,ver):
11+
for j in range(0,ver):
12+
dist[i][j] = g[i][j]
13+
#Finding the shortest distance if found
14+
for k in range(0,ver):
15+
for i in range(0,ver):
16+
for j in range(0,ver):
17+
if dist[i][k] + dist[k][j] < dist[i][j] and dist[i][k]!=-1 and dist[k][j]!=-1:
18+
dist[i][j] = dist[i][k] + dist[k][j]
19+
#Prnting the complete short distance matrix
20+
print("The distance matrix is\n")
21+
for i in range(0,ver):
22+
for j in range(0,ver):
23+
if dist[i][j]>=0:
24+
print(dist[i][j],end=" ")
25+
else:
26+
print(-1,end=" ")
27+
print("\n")
28+
#Driver's code
29+
def main():
30+
print("Enter number of vertices\n")
31+
ver=int(input())
32+
graph=[]
33+
#Creating the graph
34+
print("Enter the graph\n")
35+
for i in range(ver):
36+
a =[]
37+
for j in range(ver):
38+
a.append(int(input()))
39+
graph.append(a)
40+
warshall(graph,ver)
41+
if __name__=="__main__":
42+
main()
43+
44+
45+
46+
'''
47+
Time Complexity:O(ver^3)
48+
Space Complexity:O(ver^2)
49+
Input/Output:
50+
Enter number of vertices
51+
4
52+
Enter the graph
53+
0
54+
8
55+
-1
56+
1
57+
-1
58+
0
59+
1
60+
-1
61+
4
62+
-1
63+
0
64+
-1
65+
-1
66+
2
67+
9
68+
0
69+
The distance matrix is
70+
0 3 -1 1
71+
-1 0 1 -1
72+
4 -1 0 -1
73+
-1 2 3 0
74+
'''
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
Given the Sorted Linked list head which contain duplicates, the task is
3+
to delete all the duplicate and make linked list such that every element
4+
appear just one time.
5+
Example:
6+
Original Linked list : 1 1 2 2 3
7+
Answer : 1 2 3
8+
*/
9+
10+
#include <bits/stdc++.h>
11+
using namespace std;
12+
13+
// A linked list node
14+
class Node
15+
{
16+
public:
17+
int data;
18+
Node *next;
19+
};
20+
21+
// Utility function to create a new Node
22+
void push(Node **head, int n_data)
23+
{
24+
Node *newdata = new Node();
25+
newdata->data = n_data;
26+
newdata->next = (*head);
27+
(*head) = newdata;
28+
}
29+
30+
// Function to print nodes in a given linked list.
31+
void printlinkedlist(Node *temp)
32+
{
33+
while (temp != NULL)
34+
{
35+
cout << "->" << temp->data;
36+
temp = temp->next;
37+
}
38+
}
39+
40+
// Function to delete duplicate nodes
41+
Node *delete_duplicates(Node *head)
42+
{
43+
//initalize current to head
44+
Node *curr = head;
45+
// when curr and curr->next is not NULL
46+
// enter the loop
47+
while (curr && curr->next)
48+
{
49+
// if the adjacent nodes are found to be equal
50+
if (curr->next->data == curr->data)
51+
{
52+
// then point the curr->next to one step ahead
53+
// to the existing pointer.
54+
curr->next = curr->next->next;
55+
}
56+
else
57+
{
58+
// Until the current and current->next values are
59+
// not same, keep updating current
60+
curr = curr->next;
61+
}
62+
}
63+
return head;
64+
}
65+
int main()
66+
{
67+
Node *head = NULL;
68+
int n;
69+
cout << "Enter number of Elements:";
70+
cin >> n;
71+
cout << "Enter elements:";
72+
for (int i = 0; i < n; i++)
73+
{
74+
int a;
75+
cin >> a;
76+
push(&head, a);
77+
}
78+
cout <<"Original Linked list:\n";
79+
printlinkedlist(head);
80+
cout <<"\nNew Linked List:\n";
81+
head = delete_duplicates(head);
82+
printlinkedlist(head);
83+
}
84+
85+
/*
86+
Sample Input:
87+
Enter number of Elements:5
88+
Enter elements:1 1 2 2 3
89+
90+
Sample Output:
91+
Original Linked list:
92+
->3->2->2->1->1
93+
New Linked List:
94+
->3->2->1
95+
96+
Time-Complexity: O(n)
97+
Space-Complexity: O(1)
98+
*/

0 commit comments

Comments
 (0)