Skip to content

Commit 34a4a95

Browse files
authored
Merge branch 'main' into python_pigeonhole_sort
2 parents 24ed64f + 2b1c860 commit 34a4a95

File tree

24 files changed

+3330
-3
lines changed

24 files changed

+3330
-3
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <cstring>
4+
#include <iomanip>
5+
using namespace std;
6+
7+
// Number of vertices in the graph
8+
#define N 4
9+
10+
// data structure to store graph edges
11+
struct Edge {
12+
int src, dest;
13+
};
14+
15+
// class to represent a graph object
16+
class Graph
17+
{
18+
public:
19+
// An array of vectors to represent adjacency list
20+
vector<int> adjList[N];
21+
22+
// Constructor
23+
Graph(vector<Edge> edges)
24+
{
25+
// add edges to the directed graph
26+
for (Edge edge: edges)
27+
{
28+
int src = edge.src;
29+
int dest = edge.dest;
30+
31+
adjList[src].push_back(dest);
32+
}
33+
}
34+
};
35+
36+
// C is connectivity matrix and stores transitive closure of graph
37+
// root is the topmost node in DFS tree(it is starting vertex of DFS)
38+
// descendent is current vertex to be explored in DFS
39+
// Invariant: A path already exists from root -> descendent in graph
40+
void DFS(Graph const& graph, bool C[N][N], int root, int descendent)
41+
{
42+
for (int child : graph.adjList[descendent])
43+
{
44+
// if child is an adjacent vertex of descendent, we have
45+
// found a path from root->child
46+
if (!C[root][child])
47+
{
48+
C[root][child] = true;
49+
DFS(graph, C, root, child);
50+
}
51+
}
52+
}
53+
54+
int main()
55+
{
56+
// array of graph edges as per above diagram
57+
vector<Edge> edges = {
58+
{ 0, 2 }, { 1, 0 }, { 3, 1 }
59+
};
60+
61+
// create a graph from edges
62+
Graph graph(edges);
63+
64+
// C is connectivity matrix and stores the transitive closure
65+
// of the graph. The value of C[i][j] is 1 only if a directed
66+
// path exists from vertex i to vertex j.
67+
bool C[N][N];
68+
memset(C, false, sizeof C);
69+
70+
// consider each vertex and start DFS from it
71+
for (int v = 0; v < N; v++)
72+
{
73+
C[v][v] = true;
74+
DFS(graph, C, v, v);
75+
76+
// print path info for vertex v
77+
for (int u = 0; u < N; u++)
78+
cout << left << setw(4) << C[v][u];
79+
cout << endl;
80+
}
81+
82+
return 0;
83+
}
Lines changed: 283 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,283 @@
1+
#include <iostream>
2+
#include <cstdlib>
3+
using namespace std;
4+
5+
6+
#define MAX_TREE_HT 100
7+
8+
struct MinHeapNode {
9+
char data;
10+
unsigned freq;
11+
12+
// Left and right child of this node
13+
struct MinHeapNode *left, *right;
14+
};
15+
16+
struct MinHeap {
17+
18+
// Current size of min heap
19+
unsigned size;
20+
21+
// capacity of min heap
22+
unsigned capacity;
23+
24+
// Attay of minheap node pointers
25+
struct MinHeapNode** array;
26+
};
27+
28+
struct MinHeapNode* newNode(char data, unsigned freq)
29+
{
30+
struct MinHeapNode* temp
31+
= (struct MinHeapNode*)malloc
32+
(sizeof(struct MinHeapNode));
33+
34+
temp->left = temp->right = NULL;
35+
temp->data = data;
36+
temp->freq = freq;
37+
38+
return temp;
39+
}
40+
41+
struct MinHeap* createMinHeap(unsigned capacity)
42+
43+
{
44+
45+
struct MinHeap* minHeap
46+
= (struct MinHeap*)malloc(sizeof(struct MinHeap));
47+
48+
49+
minHeap->size = 0;
50+
51+
minHeap->capacity = capacity;
52+
53+
minHeap->array
54+
= (struct MinHeapNode**)malloc(minHeap->
55+
capacity * sizeof(struct MinHeapNode*));
56+
return minHeap;
57+
}
58+
59+
// A utility function to
60+
// swap two min heap nodes
61+
void swapMinHeapNode(struct MinHeapNode** a,
62+
struct MinHeapNode** b)
63+
64+
{
65+
66+
struct MinHeapNode* t = *a;
67+
*a = *b;
68+
*b = t;
69+
}
70+
71+
// The standard minHeapify function.
72+
void minHeapify(struct MinHeap* minHeap, int idx)
73+
74+
{
75+
76+
int smallest = idx;
77+
int left = 2 * idx + 1;
78+
int right = 2 * idx + 2;
79+
80+
if (left < minHeap->size && minHeap->array[left]->
81+
freq < minHeap->array[smallest]->freq)
82+
smallest = left;
83+
84+
if (right < minHeap->size && minHeap->array[right]->
85+
freq < minHeap->array[smallest]->freq)
86+
smallest = right;
87+
88+
if (smallest != idx) {
89+
swapMinHeapNode(&minHeap->array[smallest],
90+
&minHeap->array[idx]);
91+
minHeapify(minHeap, smallest);
92+
}
93+
}
94+
95+
// A utility function to check
96+
// if size of heap is 1 or not
97+
int isSizeOne(struct MinHeap* minHeap)
98+
{
99+
100+
return (minHeap->size == 1);
101+
}
102+
103+
// A standard function to extract
104+
// minimum value node from heap
105+
struct MinHeapNode* extractMin(struct MinHeap* minHeap)
106+
107+
{
108+
109+
struct MinHeapNode* temp = minHeap->array[0];
110+
minHeap->array[0]
111+
= minHeap->array[minHeap->size - 1];
112+
113+
--minHeap->size;
114+
minHeapify(minHeap, 0);
115+
116+
return temp;
117+
}
118+
119+
// A utility function to insert
120+
// a new node to Min Heap
121+
void insertMinHeap(struct MinHeap* minHeap,
122+
struct MinHeapNode* minHeapNode)
123+
124+
{
125+
126+
++minHeap->size;
127+
int i = minHeap->size - 1;
128+
129+
while (i && minHeapNode->freq < minHeap->array[(i - 1) / 2]->freq) {
130+
131+
minHeap->array[i] = minHeap->array[(i - 1) / 2];
132+
i = (i - 1) / 2;
133+
}
134+
135+
minHeap->array[i] = minHeapNode;
136+
}
137+
138+
// A standard function to build min heap
139+
void buildMinHeap(struct MinHeap* minHeap)
140+
141+
{
142+
143+
int n = minHeap->size - 1;
144+
int i;
145+
146+
for (i = (n - 1) / 2; i >= 0; --i)
147+
minHeapify(minHeap, i);
148+
}
149+
150+
// A utility function to print an array of size n
151+
void printArr(int arr[], int n)
152+
{
153+
int i;
154+
for (i = 0; i < n; ++i)
155+
cout<< arr[i];
156+
157+
cout<<"\n";
158+
}
159+
160+
// Utility function to check if this node is leaf
161+
int isLeaf(struct MinHeapNode* root)
162+
163+
{
164+
165+
return !(root->left) && !(root->right);
166+
}
167+
168+
// Creates a min heap of capacity
169+
// equal to size and inserts all character of
170+
// data[] in min heap. Initially size of
171+
// min heap is equal to capacity
172+
struct MinHeap* createAndBuildMinHeap(char data[], int freq[], int size)
173+
174+
{
175+
176+
struct MinHeap* minHeap = createMinHeap(size);
177+
178+
for (int i = 0; i < size; ++i)
179+
minHeap->array[i] = newNode(data[i], freq[i]);
180+
181+
minHeap->size = size;
182+
buildMinHeap(minHeap);
183+
184+
return minHeap;
185+
}
186+
187+
// The main function that builds Huffman tree
188+
struct MinHeapNode* buildHuffmanTree(char data[], int freq[], int size)
189+
190+
{
191+
struct MinHeapNode *left, *right, *top;
192+
193+
// Step 1: Create a min heap of capacity
194+
// equal to size. Initially, there are
195+
// modes equal to size.
196+
struct MinHeap* minHeap = createAndBuildMinHeap(data, freq, size);
197+
198+
// Iterate while size of heap doesn't become 1
199+
while (!isSizeOne(minHeap)) {
200+
201+
// Step 2: Extract the two minimum
202+
// freq items from min heap
203+
left = extractMin(minHeap);
204+
right = extractMin(minHeap);
205+
206+
// Step 3: Create a new internal
207+
// node with frequency equal to the
208+
// sum of the two nodes frequencies.
209+
// Make the two extracted node as
210+
// left and right children of this new node.
211+
// Add this node to the min heap
212+
// '$' is a special value for internal nodes, not used
213+
top = newNode('$', left->freq + right->freq);
214+
215+
top->left = left;
216+
top->right = right;
217+
218+
insertMinHeap(minHeap, top);
219+
}
220+
221+
// Step 4: The remaining node is the
222+
// root node and the tree is complete.
223+
return extractMin(minHeap);
224+
}
225+
226+
// Prints huffman codes from the root of Huffman Tree.
227+
// It uses arr[] to store codes
228+
void printCodes(struct MinHeapNode* root, int arr[], int top)
229+
230+
{
231+
232+
// Assign 0 to left edge and recur
233+
if (root->left) {
234+
235+
arr[top] = 0;
236+
printCodes(root->left, arr, top + 1);
237+
}
238+
239+
// Assign 1 to right edge and recur
240+
if (root->right) {
241+
242+
arr[top] = 1;
243+
printCodes(root->right, arr, top + 1);
244+
}
245+
246+
// If this is a leaf node, then
247+
// it contains one of the input
248+
// characters, print the character
249+
// and its code from arr[]
250+
if (isLeaf(root)) {
251+
252+
cout<< root->data <<": ";
253+
printArr(arr, top);
254+
}
255+
}
256+
257+
void HuffmanCodes(char data[], int freq[], int size)
258+
259+
{
260+
// Construct Huffman Tree
261+
struct MinHeapNode* root
262+
= buildHuffmanTree(data, freq, size);
263+
264+
// Print Huffman codes using
265+
// the Huffman tree built above
266+
int arr[MAX_TREE_HT], top = 0;
267+
268+
printCodes(root, arr, top);
269+
}
270+
271+
// Driver program to test above functions
272+
int main()
273+
{
274+
275+
char arr[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
276+
int freq[] = { 5, 9, 12, 13, 16, 45 };
277+
278+
int size = sizeof(arr) / sizeof(arr[0]);
279+
280+
HuffmanCodes(arr, freq, size);
281+
282+
return 0;
283+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <opencv2/opencv.hpp>
2+
using namespace cv;
3+
4+
int main( int argc, char** argv )
5+
{
6+
char* ImageFile = argv[1]; //image file
7+
Mat image; //mat object for storing data
8+
image = imread( ImageFile, IMREAD_COLOR ); //read the image file
9+
if( argc != 2 || !image.data )
10+
{
11+
printf( " No image data \n " );
12+
return -1;
13+
}
14+
Mat gray_image;
15+
cvtColor( image, gray_image, COLOR_BGR2GRAY ); //convert image from colour to grey
16+
imwrite( "/home/crmgogo/Pictures/greyImage.png", gray_image );
17+
namedWindow( ImageFile, WINDOW_AUTOSIZE );
18+
namedWindow( "Gray image", WINDOW_AUTOSIZE );
19+
imshow( ImageFile, image ); //show window containing images
20+
imshow( "Gray image", gray_image );
21+
waitKey(0); //to exit
22+
return 0;
23+
}

0 commit comments

Comments
 (0)