From c401d31c480c000c1e0df0bf38e71601d42368c2 Mon Sep 17 00:00:00 2001 From: Shruti Avhad <76914026+Shruti-codes236@users.noreply.github.com> Date: Tue, 11 Oct 2022 00:28:47 +0530 Subject: [PATCH 1/8] Cycle Detection in Linked Lists --- cycle_detection.cpp | 150 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 cycle_detection.cpp diff --git a/cycle_detection.cpp b/cycle_detection.cpp new file mode 100644 index 0000000..1965e97 --- /dev/null +++ b/cycle_detection.cpp @@ -0,0 +1,150 @@ +/* + CYCLE DETECTION + Given a linked list, detect if a cycle exists + Print Yes if it exists, else No +*/ +#include + +using namespace std; + +class SinglyLinkedListNode +{ +public: + int data; + SinglyLinkedListNode *next; + + SinglyLinkedListNode(int node_data) + { + this->data = node_data; + this->next = nullptr; + } +}; + +class SinglyLinkedList +{ +public: + SinglyLinkedListNode *head; + SinglyLinkedListNode *tail; + + SinglyLinkedList() + { + this->head = nullptr; + this->tail = nullptr; + } + + void insert_node(int node_data) + { + SinglyLinkedListNode *node = new SinglyLinkedListNode(node_data); + + if (!this->head) + { + this->head = node; + } + else + { + this->tail->next = node; + } + + this->tail = node; + } +}; + +void print_singly_linked_list(SinglyLinkedListNode *node, string sep, ofstream &fout) +{ + while (node) + { + fout << node->data; + + node = node->next; + + if (node) + { + fout << sep; + } + } +} + +void free_singly_linked_list(SinglyLinkedListNode *node) +{ + while (node) + { + SinglyLinkedListNode *temp = node; + node = node->next; + + free(temp); + } +} + +string Has_Cycle(SinglyLinkedListNode *head) +{ + SinglyLinkedListNode *slow = head; + SinglyLinkedListNode *fast = head; + + while (fast != nullptr && fast->next != nullptr) + { + slow = slow->next; + fast = fast->next->next; + if (slow == fast) + { + return " Yes, Cycle exists!"; + } + } + return " No, Cycle doesn't exist!"; +} + +int main() +{ + cout << " Enter the number of linked lists : "; + int tests; + cin >> tests; + + for (int tests_itr = 0; tests_itr < tests; tests_itr++) + { + cout<<" Enter the number of nodes : "; + int index; + cin >> index; + + + + SinglyLinkedList *llist = new SinglyLinkedList(); + + cout << " For every LL enter the list count : "; + int llist_count; + cin >> llist_count; + + cout << " Then enter the nodes starting from the head in sequential order,\n every node pointing to the next one : "; + + for (int i = 0; i < llist_count; i++) + { + int llist_item; + cin >> llist_item; + + llist->insert_node(llist_item); + } + + SinglyLinkedListNode *extra = new SinglyLinkedListNode(-1); + SinglyLinkedListNode *temp = llist->head; + + for (int i = 0; i < llist_count; i++) + { + if (i == index) + { + extra = temp; + } + + if (i != llist_count - 1) + { + temp = temp->next; + } + } + + temp->next = extra; + + string result = Has_Cycle(llist->head); + + cout << result << "\n"; + } + return 0; +} + +/* This code is contributed by Shruti Avhad */ From afd7c42749a583cf2b6460b96474ca3e354ce4c2 Mon Sep 17 00:00:00 2001 From: Shruti Avhad <76914026+Shruti-codes236@users.noreply.github.com> Date: Wed, 12 Oct 2022 18:43:47 +0530 Subject: [PATCH 2/8] Top-View of a given Binary Tree in Python --- Top_View.py | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 Top_View.py diff --git a/Top_View.py b/Top_View.py new file mode 100644 index 0000000..b5e10a9 --- /dev/null +++ b/Top_View.py @@ -0,0 +1,66 @@ +class Node: + def __init__(self, info): + self.info = info + self.left = None + self.right = None + self.level = None + + def __str__(self): + return str(self.info) + +class BinarySearchTree: + def __init__(self): + self.root = None + + def create(self, val): + if self.root == None: + self.root = Node(val) + else: + current = self.root + + while True: + if val < current.info: + if current.left: + current = current.left + else: + current.left = Node(val) + break + elif val > current.info: + if current.right: + current = current.right + else: + current.right = Node(val) + break + else: + break + +def traverse(root,coord_info_map,x,y): + + if x not in coord_info_map or y < coord_info_map[x][1]: + coord_info_map[x] = [root.info,y] + + if root.left: + traverse(root.left,coord_info_map, x - 1, y + 1) + if root.right: + traverse(root.right,coord_info_map, x + 1, y + 1) + +def topView(root): + x = 0 + y = 0 + coord_info_map = {} + traverse(root, coord_info_map, x, y) + for key in sorted(coord_info_map): + print(coord_info_map[key][0], end=" ") + + + +tree = BinarySearchTree() +print('Input the number of tree nodes :') +t = int(input()) +print('Input the tree node values in preorder :') +arr = list(map(int, input().split())) + +for i in range(t): + tree.create(arr[i]) + +topView(tree.root) \ No newline at end of file From dcf461a6b36bd161f65d61695be0771a33cbfce6 Mon Sep 17 00:00:00 2001 From: Shruti Avhad <76914026+Shruti-codes236@users.noreply.github.com> Date: Thu, 13 Oct 2022 02:13:08 +0530 Subject: [PATCH 3/8] Delete Top_View.py --- Top_View.py | 66 ----------------------------------------------------- 1 file changed, 66 deletions(-) delete mode 100644 Top_View.py diff --git a/Top_View.py b/Top_View.py deleted file mode 100644 index b5e10a9..0000000 --- a/Top_View.py +++ /dev/null @@ -1,66 +0,0 @@ -class Node: - def __init__(self, info): - self.info = info - self.left = None - self.right = None - self.level = None - - def __str__(self): - return str(self.info) - -class BinarySearchTree: - def __init__(self): - self.root = None - - def create(self, val): - if self.root == None: - self.root = Node(val) - else: - current = self.root - - while True: - if val < current.info: - if current.left: - current = current.left - else: - current.left = Node(val) - break - elif val > current.info: - if current.right: - current = current.right - else: - current.right = Node(val) - break - else: - break - -def traverse(root,coord_info_map,x,y): - - if x not in coord_info_map or y < coord_info_map[x][1]: - coord_info_map[x] = [root.info,y] - - if root.left: - traverse(root.left,coord_info_map, x - 1, y + 1) - if root.right: - traverse(root.right,coord_info_map, x + 1, y + 1) - -def topView(root): - x = 0 - y = 0 - coord_info_map = {} - traverse(root, coord_info_map, x, y) - for key in sorted(coord_info_map): - print(coord_info_map[key][0], end=" ") - - - -tree = BinarySearchTree() -print('Input the number of tree nodes :') -t = int(input()) -print('Input the tree node values in preorder :') -arr = list(map(int, input().split())) - -for i in range(t): - tree.create(arr[i]) - -topView(tree.root) \ No newline at end of file From ef592a9b3b2d1b98a49e51e3f58ee7e15d065ef4 Mon Sep 17 00:00:00 2001 From: Shruti Avhad <76914026+Shruti-codes236@users.noreply.github.com> Date: Thu, 13 Oct 2022 12:46:24 +0530 Subject: [PATCH 4/8] Top-View of a binary tree in Python --- Top_View.py | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 Top_View.py diff --git a/Top_View.py b/Top_View.py new file mode 100644 index 0000000..8316816 --- /dev/null +++ b/Top_View.py @@ -0,0 +1,72 @@ +""" + Top-View Of a Binary Tree in Python + Given a Binary tree in preorder traversal, output the top view of the tree +""" +class Node: + def __init__(self, info): + self.info = info + self.left = None + self.right = None + self.level = None + + def __str__(self): + return str(self.info) + +class BinarySearchTree: + def __init__(self): + self.root = None + + def create(self, val): + if self.root == None: + self.root = Node(val) + else: + current = self.root + + while True: + if val < current.info: + if current.left: + current = current.left + else: + current.left = Node(val) + break + elif val > current.info: + if current.right: + current = current.right + else: + current.right = Node(val) + break + else: + break + +def traverse(root,coord_info_map,x,y): + + if x not in coord_info_map or y < coord_info_map[x][1]: + coord_info_map[x] = [root.info,y] + + if root.left: + traverse(root.left,coord_info_map, x - 1, y + 1) + if root.right: + traverse(root.right,coord_info_map, x + 1, y + 1) + +def topView(root): + x = 0 + y = 0 + coord_info_map = {} + traverse(root, coord_info_map, x, y) + for key in sorted(coord_info_map): + print(coord_info_map[key][0], end=" ") + + + +tree = BinarySearchTree() +print('Input the number of tree nodes :') +t = int(input()) +print('Input the tree node values in preorder :') +arr = list(map(int, input().split())) + +for i in range(t): + tree.create(arr[i]) + +topView(tree.root) + +"""This Code is contributed By Shruti Avhad""" \ No newline at end of file From 26a637b2d1b3b7e6c150e3f6d21ed83e22911a34 Mon Sep 17 00:00:00 2001 From: Shruti Avhad <76914026+Shruti-codes236@users.noreply.github.com> Date: Fri, 14 Oct 2022 01:10:08 +0530 Subject: [PATCH 5/8] Delete Top_View.py --- Top_View.py | 72 ----------------------------------------------------- 1 file changed, 72 deletions(-) delete mode 100644 Top_View.py diff --git a/Top_View.py b/Top_View.py deleted file mode 100644 index 8316816..0000000 --- a/Top_View.py +++ /dev/null @@ -1,72 +0,0 @@ -""" - Top-View Of a Binary Tree in Python - Given a Binary tree in preorder traversal, output the top view of the tree -""" -class Node: - def __init__(self, info): - self.info = info - self.left = None - self.right = None - self.level = None - - def __str__(self): - return str(self.info) - -class BinarySearchTree: - def __init__(self): - self.root = None - - def create(self, val): - if self.root == None: - self.root = Node(val) - else: - current = self.root - - while True: - if val < current.info: - if current.left: - current = current.left - else: - current.left = Node(val) - break - elif val > current.info: - if current.right: - current = current.right - else: - current.right = Node(val) - break - else: - break - -def traverse(root,coord_info_map,x,y): - - if x not in coord_info_map or y < coord_info_map[x][1]: - coord_info_map[x] = [root.info,y] - - if root.left: - traverse(root.left,coord_info_map, x - 1, y + 1) - if root.right: - traverse(root.right,coord_info_map, x + 1, y + 1) - -def topView(root): - x = 0 - y = 0 - coord_info_map = {} - traverse(root, coord_info_map, x, y) - for key in sorted(coord_info_map): - print(coord_info_map[key][0], end=" ") - - - -tree = BinarySearchTree() -print('Input the number of tree nodes :') -t = int(input()) -print('Input the tree node values in preorder :') -arr = list(map(int, input().split())) - -for i in range(t): - tree.create(arr[i]) - -topView(tree.root) - -"""This Code is contributed By Shruti Avhad""" \ No newline at end of file From fc757f1bcdfb43e6019e2ad94df86519bb85c8d0 Mon Sep 17 00:00:00 2001 From: Shruti Avhad <76914026+Shruti-codes236@users.noreply.github.com> Date: Fri, 14 Oct 2022 01:10:19 +0530 Subject: [PATCH 6/8] Delete cycle_detection.cpp --- cycle_detection.cpp | 150 -------------------------------------------- 1 file changed, 150 deletions(-) delete mode 100644 cycle_detection.cpp diff --git a/cycle_detection.cpp b/cycle_detection.cpp deleted file mode 100644 index 1965e97..0000000 --- a/cycle_detection.cpp +++ /dev/null @@ -1,150 +0,0 @@ -/* - CYCLE DETECTION - Given a linked list, detect if a cycle exists - Print Yes if it exists, else No -*/ -#include - -using namespace std; - -class SinglyLinkedListNode -{ -public: - int data; - SinglyLinkedListNode *next; - - SinglyLinkedListNode(int node_data) - { - this->data = node_data; - this->next = nullptr; - } -}; - -class SinglyLinkedList -{ -public: - SinglyLinkedListNode *head; - SinglyLinkedListNode *tail; - - SinglyLinkedList() - { - this->head = nullptr; - this->tail = nullptr; - } - - void insert_node(int node_data) - { - SinglyLinkedListNode *node = new SinglyLinkedListNode(node_data); - - if (!this->head) - { - this->head = node; - } - else - { - this->tail->next = node; - } - - this->tail = node; - } -}; - -void print_singly_linked_list(SinglyLinkedListNode *node, string sep, ofstream &fout) -{ - while (node) - { - fout << node->data; - - node = node->next; - - if (node) - { - fout << sep; - } - } -} - -void free_singly_linked_list(SinglyLinkedListNode *node) -{ - while (node) - { - SinglyLinkedListNode *temp = node; - node = node->next; - - free(temp); - } -} - -string Has_Cycle(SinglyLinkedListNode *head) -{ - SinglyLinkedListNode *slow = head; - SinglyLinkedListNode *fast = head; - - while (fast != nullptr && fast->next != nullptr) - { - slow = slow->next; - fast = fast->next->next; - if (slow == fast) - { - return " Yes, Cycle exists!"; - } - } - return " No, Cycle doesn't exist!"; -} - -int main() -{ - cout << " Enter the number of linked lists : "; - int tests; - cin >> tests; - - for (int tests_itr = 0; tests_itr < tests; tests_itr++) - { - cout<<" Enter the number of nodes : "; - int index; - cin >> index; - - - - SinglyLinkedList *llist = new SinglyLinkedList(); - - cout << " For every LL enter the list count : "; - int llist_count; - cin >> llist_count; - - cout << " Then enter the nodes starting from the head in sequential order,\n every node pointing to the next one : "; - - for (int i = 0; i < llist_count; i++) - { - int llist_item; - cin >> llist_item; - - llist->insert_node(llist_item); - } - - SinglyLinkedListNode *extra = new SinglyLinkedListNode(-1); - SinglyLinkedListNode *temp = llist->head; - - for (int i = 0; i < llist_count; i++) - { - if (i == index) - { - extra = temp; - } - - if (i != llist_count - 1) - { - temp = temp->next; - } - } - - temp->next = extra; - - string result = Has_Cycle(llist->head); - - cout << result << "\n"; - } - return 0; -} - -/* This code is contributed by Shruti Avhad */ From d274c2f2c77da373ef2f92a9c34bda48b021bccf Mon Sep 17 00:00:00 2001 From: Shruti Avhad <76914026+Shruti-codes236@users.noreply.github.com> Date: Fri, 14 Oct 2022 01:12:39 +0530 Subject: [PATCH 7/8] N-Queens in C++ --- N-Queens.cpp | 260 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 260 insertions(+) create mode 100644 N-Queens.cpp diff --git a/N-Queens.cpp b/N-Queens.cpp new file mode 100644 index 0000000..2f427b7 --- /dev/null +++ b/N-Queens.cpp @@ -0,0 +1,260 @@ +/* + N-Queens problem + Given an N*N Chess-Board, show places where N Queens should be placed, + So that they don't attack each other. + Denote if solution Doesn't exist. +*/ +#include +#define Max 100 + +using namespace std; + +int n; + +void Board_print(int board[][Max]) +{ + for (int i = 0; i < n; i++) + { + cout << " "; + for (int j = 0; j < n; j++) + { + cout << board[i][j] << " "; + } + cout << "\n"; + } +} + +bool Compare_States(int *state1, + int *state2) +{ + + for (int i = 0; i < n; i++) + { + if (state1[i] != state2[i]) + { + return false; + } + } + return true; +} + +void randomConfig(int board[][Max], int *state) +{ + + for (int i = 0; i < n; i++) + { + state[i] = rand() % n; + board[state[i]][i] = 1; + } +} + +void fill(int board[][Max], int value) +{ + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + board[i][j] = value; + } + } +} + +int calculateObjective(int board[][Max], + int *state) +{ + int attacking = 0; + + int row, col; + + for (int i = 0; i < n; i++) + { + + row = state[i], col = i - 1; + while (col >= 0 && board[row][col] != 1) + { + col--; + } + if (col >= 0 && board[row][col] == 1) + { + attacking++; + } + + row = state[i], col = i + 1; + while (col < n && board[row][col] != 1) + { + col++; + } + if (col < n && board[row][col] == 1) + { + attacking++; + } + + row = state[i] - 1, col = i - 1; + while (col >= 0 && row >= 0 && board[row][col] != 1) + { + col--; + row--; + } + if (col >= 0 && row >= 0 && board[row][col] == 1) + { + attacking++; + } + + row = state[i] + 1, col = i + 1; + while (col < n && row < n && board[row][col] != 1) + { + col++; + row++; + } + if (col < n && row < n && board[row][col] == 1) + { + attacking++; + } + + row = state[i] + 1, col = i - 1; + while (col >= 0 && row < n && board[row][col] != 1) + { + col--; + row++; + } + if (col >= 0 && row < n && board[row][col] == 1) + { + attacking++; + } + + row = state[i] - 1, col = i + 1; + while (col < n && row >= 0 && board[row][col] != 1) + { + col++; + row--; + } + if (col < n && row >= 0 && board[row][col] == 1) + { + attacking++; + } + } + + return (int)(attacking / 2); +} + +void generateBoard(int board[][Max], int *state) +{ + + fill(board, 0); + for (int i = 0; i < n; i++) + { + board[state[i]][i] = 1; + } +} + +void Copy_State(int *state1, int *state2) +{ + + for (int i = 0; i < n; i++) + { + state1[i] = state2[i]; + } +} + +void Get_Neighbours(int board[][Max], + int *state) +{ + + int board_op[Max][Max]; + int state_op[n]; + + Copy_State(state_op, state); + generateBoard(board_op, state_op); + + int opObjective = calculateObjective(board_op, state_op); + + int NeighbourBoard[Max][Max]; + int NeighbourState[n]; + + Copy_State(NeighbourState, state); + generateBoard(NeighbourBoard, NeighbourState); + + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + + if (j != state[i]) + { + + NeighbourState[i] = j; + NeighbourBoard[NeighbourState[i]][i] = 1; + NeighbourBoard[state[i]][i] = 0; + + int temp = calculateObjective(NeighbourBoard, NeighbourState); + + if (temp <= opObjective) + { + opObjective = temp; + Copy_State(state_op, NeighbourState); + generateBoard(board_op, state_op); + } + + NeighbourBoard[NeighbourState[i]][i] = 0; + NeighbourState[i] = state[i]; + NeighbourBoard[state[i]][i] = 1; + } + } + } + + Copy_State(state, state_op); + fill(board, 0); + generateBoard(board, state); +} + +void hillClimbing(int board[][Max], + int *state) +{ + + int neighbourBoard[Max][Max] = {}; + int neighbourState[n]; + + Copy_State(neighbourState, state); + generateBoard(neighbourBoard, neighbourState); + + while (true) + { + + Copy_State(state, neighbourState); + generateBoard(board, state); + + Get_Neighbours(neighbourBoard, neighbourState); + + if (Compare_States(state, neighbourState)) + { + + Board_print(board); + break; + } + else if (calculateObjective(board, state) == calculateObjective(neighbourBoard, neighbourState)) + { + neighbourState[rand() % n] = rand() % n; + generateBoard(neighbourBoard, neighbourState); + } + } +} + +int main() +{ + cout << "Enter the board size: "; + cin >> n; + if (n < 4) + { + cout << "\nNo possible solution\n"; + return 0; + } + int state[Max] = {}; + int board[Max][Max] = {}; + + randomConfig(board, state); + + hillClimbing(board, state); + + return 0; +} +/* This code is Contributed By Shruti Avhad */ \ No newline at end of file From 2ad6cdeba10bb40491bdfabea01c89c08a2dbe5d Mon Sep 17 00:00:00 2001 From: Shruti Avhad <76914026+Shruti-codes236@users.noreply.github.com> Date: Fri, 14 Oct 2022 01:14:07 +0530 Subject: [PATCH 8/8] Rename N-Queens.cpp to c++/N-Queens.cpp --- N-Queens.cpp => c++/N-Queens.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename N-Queens.cpp => c++/N-Queens.cpp (99%) diff --git a/N-Queens.cpp b/c++/N-Queens.cpp similarity index 99% rename from N-Queens.cpp rename to c++/N-Queens.cpp index 2f427b7..3763de8 100644 --- a/N-Queens.cpp +++ b/c++/N-Queens.cpp @@ -257,4 +257,4 @@ int main() return 0; } -/* This code is Contributed By Shruti Avhad */ \ No newline at end of file +/* This code is Contributed By Shruti Avhad */