Complexity Analysis and Circular Linked List, Quizzes of Data Structures and Algorithms

An explanation of worst-case complexity in computer science and how to analyze it in the context of two loops and a nested loop. Additionally, it introduces a circular linked list and provides C++ code for implementing a circular linked list, including functions for pushing, inserting after, appending, and printing the list.

Typology: Quizzes

2019/2020

Uploaded on 12/12/2020

cpen-22-umair
cpen-22-umair 🇵🇰

4 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Assignment : Data structure and Algorithm
Student Name : Umair Khalid
Reg# : Cpen18111022
Submitted to : Engr. Hammad Shahab
----------------------------------------------------------------------------------------------------------
Q.1 Worst-case complexity.
In computer science, the complexity of the worst case scenario (usually the asymptomatic
symbol) measures the resources (such as running time, memory) that are given input at an
arbitrary size by the algorithm (usually N or NK). Also known as). It sets a limit on the resources
required by the algorithm.
1. Two loops in a row:
1. for (x = 0; x < A; x++) { sequence of statements
2. } for (y = 0; y < B; y++) { sequence of statements }
The first loop is O (A) and the second loop is O (B). Since we don't know which one is bigger, we
say it's O (A + B). It can also be written as O (maximum (A,B)). In the case where the second
loop goes to A instead of B, the complication is O (A). We can see it from the expression above.
O (A + B) becomes O (2A) and when we drop it permanently it becomes O (A). O (Max (A, B))
becomes O (Max (A, A)) which is O (A).
2. A nested loop followed by a non-nested loop:
1. for (x = 0; x < A; x++) { for (y = 0; y < A; y++) { sequence of statements}}
2. for (z = 0; z < A; z++) { sequence of statements
The first set of nested loops is O(A^2) and the second loop is O(A). This is O(max(A2,A)) which is O(A2).
Q.2 : Circular link list.
Circular LinkedIn List is a variant of a linked list in which the first element refers to the last
element and the last element refers to the first element. Both single linked list and double
linked list can be made circular linked list.
C++ Code
#include <bits/stdc++.h>
using namespace std;
class Node
pf3
pf4
pf5

Partial preview of the text

Download Complexity Analysis and Circular Linked List and more Quizzes Data Structures and Algorithms in PDF only on Docsity!

Assignment : Data structure and Algorithm

Student Name : Umair Khalid

Reg# : Cpen

Submitted to : Engr. Hammad Shahab

Q.1 Worst-case complexity.

In computer science, the complexity of the worst case scenario (usually the asymptomatic symbol) measures the resources (such as running time, memory) that are given input at an arbitrary size by the algorithm (usually N or NK). Also known as). It sets a limit on the resources required by the algorithm.

1. Two loops in a row:

  1. for (x = 0; x < A; x++) { sequence of statements
  2. } for (y = 0; y < B; y++) { sequence of statements } The first loop is O (A) and the second loop is O (B). Since we don't know which one is bigger, we say it's O (A + B). It can also be written as O (maximum (A,B)). In the case where the second loop goes to A instead of B, the complication is O (A). We can see it from the expression above. O (A + B) becomes O (2A) and when we drop it permanently it becomes O (A). O (Max (A, B)) becomes O (Max (A, A)) which is O (A).

2. A nested loop followed by a non-nested loop:

  1. for (x = 0; x < A; x++) { for (y = 0; y < A; y++) { sequence of statements}}
  2. for (z = 0; z < A; z++) { sequence of statements The first set of nested loops is O(A^2) and the second loop is O(A). This is O(max(A^2 ,A)) which is O(A^2 ).

Q.2 : Circular link list.

Circular LinkedIn List is a variant of a linked list in which the first element refers to the last element and the last element refers to the first element. Both single linked list and double linked list can be made circular linked list.

C++ Code

#include <bits/stdc++.h> using namespace std; class Node

public: int data; Node* next; Node* prev; }; void push(Node** head_ref, int new_data) { Node* new_node = new Node(); new_node->next = (head_ref); new_node->data = new_data; new_node->prev = NULL; if ((head_ref) != NULL) (head_ref)->prev = new_node; (head_ref) = new_node; } void insertAfter(Node* prev_node, int new_data) { if (prev_node == NULL) { cout<<"the given previous node cannot be NULL"; return; } Node* new_node = new Node(); new_node->data = new_data;

Node* last; cout<<"\nTraversal in forward direction \n"; while (node != NULL) { cout<<" "<<node->data<<" "; last = node; node = node->next; } cout<<"\nTraversal in reverse direction \n"; while (last != NULL) { cout<<" "<<last->data<<" "; last = last->prev; } } int main() { Node* head = NULL; push(&head, 8); push(&head, 4); append(&head,7); insertAfter(head->next, 1);

cout << "\nCreated DLL is:"; printList(head); return 0; }