Skip to content

Commit 0ca0e0f

Browse files
Merge pull request #222 from Iamtripathisatyam/main
Priority Queue
2 parents 38952c5 + cded73c commit 0ca0e0f

File tree

5 files changed

+104
-1
lines changed

5 files changed

+104
-1
lines changed

Graphs/Breadth First Search/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Two techniques are frequently used for graph traversal: These techniques are called depth-first search (DFS) and breadth-first search (BFS), although we will just talk about BFS. Breadth-first search (BFS) is used to solve many problems, including finding the shortest path in a graph or solving puzzle games such as Rubik’s Cubes.
33

44
## What is Breadth First Search in Python?
5-
Breadth-first search (BFS) is an algorithm for traversing graphs or trees. Breath-first search for trees and graphs are almost the same. BFS uses a queue data structure for finding the shortest path in a graph.
5+
Breadth-first search (BFS) is an algorithm for traversing graphs or trees. Breath-first search for trees and graphs are almost the same. BFS uses a queue data structure for finding the shortest path in a graph. We use queue to get the next vertex to start the search, and the visited array is used to mark all the nodes that have been visited, so when it appears again, we will perform BFS on that node.
66

77
## Algorithm of DFS in Python
88
The algorithm works as follows:
@@ -18,6 +18,12 @@ The algorithm works as follows:
1818
* Time complexity is `O(V+E)`, where V denotes the number of vertices and E denotes the number of edges.
1919
* Space complexity is `O(V)`.
2020

21+
## Applications of Breadth First Search
22+
* Crawlers in Search Engines
23+
* Peer-to-Peer Networking
24+
* GPS Navigation Systems
25+
* Ford-Fulkerson algorithm
26+
2127
## Input & Output
2228
### Input:
2329

Queues/Priority Queue/Images/hey

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
30.7 KB
Loading

Queues/Priority Queue/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<p align='center'>
2+
<img width=60% src="../Priority Queue/Images/priority.jpg">
3+
</p>
4+
5+
## Overview:
6+
A unique kind of queue called a priority queue has elements that are each assigned a priority value. Additionally, components are treated according to their importance. In other words, more important factors come first.
7+
8+
## What is Priority Queue?
9+
There must be a priority assigned to each item in the queue.
10+
Depending on the priority order chosen by the user—that is, whether the user considers a lower number to be higher priority or a higher number to be greater priority—higher or lower priority items must be dequeued before lower or higher priority elements, respectively.
11+
The first element that entered the priority queue first will be the first to be deleted if two items in the queue have the same priority value. This is known as the first in, first out, rule.
12+
13+
## Types of Priority Queue
14+
#### Ascending Order Priority Queue:
15+
The items in the priority queue must be similar, which means they must be either less than, equal to, or higher than one another, as was previously stated. All the components are compared to one another in an ascending order priority queue according to the following rule. The priority is applied with a higher priority to smaller elements (or numbers).
16+
17+
#### Descending Order Priority Queue:
18+
All the components are compared to one another in descending order priority queue according to the following rule. The priority increases with the size of the element (or number). Eaual to one another or larger than it. All the components are compared to one another in an ascending order priority queue according to the following rule. The priority is applied with a higher priority to smaller elements (or numbers).
19+
20+
## Why are Heaps preferred over Binary Search Trees?
21+
While the Self Balancing BST requires O(nLogn) time, heap construction may be completed in O(N) time. Pointers are not used anymore since heaps are built via arrays.
22+
23+
## Application of Priority Queue
24+
* Djikstra's Algorithm uses t to determine the shortest route between graph nodes.
25+
* In order to determine the Minimum Spanning Tree in a weighted undirected graph, it is employed in Prim's Algorithm.
26+
* Heap Sort uses it to order the heap data structure.
27+
* It is employed in the data compression algorithm Huffman Coding.
28+
29+
## Complexity of Priority queue
30+
#### Time complexity:
31+
* Insertion : O(log n)O(logn)
32+
* Peek : O(1)O(1)
33+
* Deletion : O(log n)O(logn)
34+
35+
## Read More
36+
To read more: [Click here](https://www.scaler.com/topics/data-structures/priority-queue-in-data-structure/)

Queues/Priority Queue/priority.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# To heapify
2+
def heapify(array, num, x):
3+
# Find the largest among root, left child and right child
4+
max = x
5+
left = 2 * x + 1
6+
right = 2 * x + 2
7+
8+
if left < num and array[x] < array[left]:
9+
max = left
10+
11+
if right < num and array[max] < array[right]:
12+
max = right
13+
14+
# If root is not largest, swap and continue heapifying
15+
if max != x:
16+
array[x], array[max] = array[max], array[x]
17+
heapify(array, num, max)
18+
19+
20+
# To insert an element
21+
def insert(nums, next):
22+
s = len(nums)
23+
if s == 0:
24+
nums.append(next)
25+
else:
26+
nums.append(next)
27+
for i in range((s // 2) - 1, -1, -1):
28+
heapify(nums, s, i)
29+
30+
31+
# To delete an element
32+
def delete(nums, n):
33+
s = len(nums)
34+
i = 0
35+
for i in range(0, s):
36+
if n == nums[i]:
37+
break
38+
39+
nums[i], nums[s - 1] = nums[s - 1], nums[i]
40+
41+
nums.remove(s - 1)
42+
43+
for i in range((len(nums) // 2) - 1, -1, -1):
44+
heapify(nums, len(nums), i)
45+
46+
47+
array = []
48+
49+
insert(array, 1)
50+
insert(array, 4)
51+
insert(array, 2)
52+
insert(array, 7)
53+
insert(array, 8)
54+
insert(array, 5)
55+
insert(array, 6)
56+
57+
print("Max-Heap array: " + str(array))
58+
59+
delete(array, 6)
60+
print("After deletion: " + str(array))

0 commit comments

Comments
 (0)