# C++ Programming Code Examples

## C++ > Computer Graphics Code Examples

### Program to Find the Largest clique in a Planar Graph

/* Program to Find the Largest clique in a Planar Graph This is a C++ Program to find the cliques of size k in a a graph. An undirected graph is formed by a finite set of vertices and a set of unordered pairs of vertices, which are called edges. By convention, in algorithm analysis, the number of vertices in the graph is denoted by n and the number of edges is denoted by m. A clique in a graph G is a complete subgraph of G; that is, it is a subset S of the vertices such that every two vertices in S are connected by an edge in G. A maximal clique is a clique to which no more vertices can be added; a maximum clique is a clique that includes the largest possible number of vertices, and the clique number ?(G) is the number of vertices in a maximum clique of G. */ #include <iostream> #include <fstream> #include <string> #include <vector> using namespace std; bool removable(vector<int> neighbor, vector<int> cover); int max_removable(vector<vector<int> > neighbors, vector<int> cover); vector<int> procedure_1(vector<vector<int> > neighbors, vector<int> cover); vector<int> procedure_2(vector<vector<int> > neighbors, vector<int> cover, int k); int cover_size(vector<int> cover); ifstream infile("graph.txt"); ofstream outfile("cliques.txt"); int main() { //Read Graph (note we work with the complement of the input graph) cout << "Clique Algorithm." << endl; int n, i, j, k, K, p, q, r, s, min, edge, counter = 0; infile >> n; vector<vector<int> > graph; for (i = 0; i < n; i++) { vector<int> row; for (j = 0; j < n; j++) { infile >> edge; if (edge == 0) row.push_back(1); else row.push_back(0); } graph.push_back(row); } //Find Neighbors vector<vector<int> > neighbors; for (i = 0; i < graph.size(); i++) { vector<int> neighbor; for (j = 0; j < graph[i].size(); j++) if (graph[i][j] == 1) neighbor.push_back(j); neighbors.push_back(neighbor); } cout << "Graph has n = " << n << " vertices." << endl; //Read maximum size of Clique wanted cout << "Find a Clique of size at least k = "; cin >> K; k = n - K; //Find Cliques bool found = false; cout << "Finding Cliques..." << endl; min = n + 1; vector<vector<int> > covers; vector<int> allcover; for (i = 0; i < graph.size(); i++) allcover.push_back(1); for (i = 0; i < allcover.size(); i++) { if (found) break; counter++; cout << counter << ". "; outfile << counter << ". "; vector<int> cover = allcover; cover[i] = 0; cover = procedure_1(neighbors, cover); s = cover_size(cover); if (s < min) min = s; if (s <= k) { outfile << "Clique (" << n - s << "): "; for (j = 0; j < cover.size(); j++) if (cover[j] == 0) outfile << j + 1 << " "; outfile << endl; cout << "Clique Size: " << n - s << endl; covers.push_back(cover); found = true; break; } for (j = 0; j < n - k; j++) cover = procedure_2(neighbors, cover, j); s = cover_size(cover); if (s < min) min = s; outfile << "Clique (" << n - s << "): "; for (j = 0; j < cover.size(); j++) if (cover[j] == 0) outfile << j + 1 << " "; outfile << endl; cout << "Clique Size: " << n - s << endl; covers.push_back(cover); if (s <= k) { found = true; break; } } //Pairwise Intersections for (p = 0; p < covers.size(); p++) { if (found) break; for (q = p + 1; q < covers.size(); q++) { if (found) break; counter++; cout << counter << ". "; outfile << counter << ". "; vector<int> cover = allcover; for (r = 0; r < cover.size(); r++) if (covers[p][r] == 0 && covers[q][r] == 0) cover[r] = 0; cover = procedure_1(neighbors, cover); s = cover_size(cover); if (s < min) min = s; if (s <= k) { outfile << "Clique (" << n - s << "): "; for (j = 0; j < cover.size(); j++) if (cover[j] == 0) outfile << j + 1 << " "; outfile << endl; cout << "Clique Size: " << n - s << endl; found = true; break; } for (j = 0; j < k; j++) cover = procedure_2(neighbors, cover, j); s = cover_size(cover); if (s < min) min = s; outfile << "Clique (" << n - s << "): "; for (j = 0; j < cover.size(); j++) if (cover[j] == 0) outfile << j + 1 << " "; outfile << endl; cout << "Clique Size: " << n - s << endl; if (s <= k) { found = true; break; } } } if (found) cout << "Found Clique of size at least " << K << "." << endl; else cout << "Could not find Clique of size at least " << K << "." << endl << "Maximum Clique size found is " << n - min << "." << endl; cout << "See cliques.txt for results." << endl; return 0; } bool removable(vector<int> neighbor, vector<int> cover) { bool check = true; for (int i = 0; i < neighbor.size(); i++) if (cover[neighbor[i]] == 0) { check = false; break; } return check; } int max_removable(vector<vector<int> > neighbors, vector<int> cover) { int r = -1, max = -1; for (int i = 0; i < cover.size(); i++) { if (cover[i] == 1 && removable(neighbors[i], cover) == true) { vector<int> temp_cover = cover; temp_cover[i] = 0; int sum = 0; for (int j = 0; j < temp_cover.size(); j++) if (temp_cover[j] == 1 && removable(neighbors[j], temp_cover) == true) sum++; if (sum > max) { max = sum; r = i; } } } return r; } vector<int> procedure_1(vector<vector<int> > neighbors, vector<int> cover) { vector<int> temp_cover = cover; int r = 0; while (r != -1) { r = max_removable(neighbors, temp_cover); if (r != -1) temp_cover[r] = 0; } return temp_cover; } vector<int> procedure_2(vector<vector<int> > neighbors, vector<int> cover, int k) { int count = 0; vector<int> temp_cover = cover; int i = 0; for (int i = 0; i < temp_cover.size(); i++) { if (temp_cover[i] == 1) { int sum = 0, index; for (int j = 0; j < neighbors[i].size(); j++) if (temp_cover[neighbors[i][j]] == 0) { index = j; sum++; } if (sum == 1 && cover[neighbors[i][index]] == 0) { temp_cover[neighbors[i][index]] = 1; temp_cover[i] = 0; temp_cover = procedure_1(neighbors, temp_cover); count++; } if (count > k) break; } } return temp_cover; } int cover_size(vector<int> cover) { int count = 0; for (int i = 0; i < cover.size(); i++) if (cover[i] == 1) count++; return count; }

Return size. Returns the number of elements in the vector. This is the number of actual objects held in the vector, which is not necessarily equal to its storage capacity. vector::size() is a library function of "vector" header, it is used to get the size of a vector, it returns the total number of elements in the vector. The dynamic array can be created by using a vector in C++. One or more elements can be inserted into or removed from the vector at the run time that increases or decreases the size of the vector. The size or length of the vector can be counted using any loop or the built-in function named size(). This function does not accept any parameter.

In computer programming, loops are used to repeat a block of code. For example, when you are displaying number from 1 to 100 you may want set the value of a variable to 1 and display it 100 times, increasing its value by 1 on each loop iteration. When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop. A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.

Access element. Returns a reference to the element at position n in the vector container. A similar member function, vector::at, has the same behavior as this operator function, except that vector::at is bound-checked and signals if the requested position is out of range by throwing an out_of_range exception. Portable programs should never call this function with an argument n that is out of range, since this causes undefined behavior. Function returns the element at the specified position in the vector.

In computer programming, we use the if statement to run a block code only when a certain condition is met. An if statement can be followed by an optional else statement, which executes when the boolean expression is false. There are three forms of if...else statements in C++: • if statement, • if...else statement, • if...else if...else statement, The if statement evaluates the condition inside the parentheses ( ). If the condition evaluates to true, the code inside the body of if is executed. If the condition evaluates to false, the code inside the body of if is skipped.

In C++, vectors are used to store elements of similar data types. However, unlike arrays, the size of a vector can grow dynamically. That is, we can change the size of the vector during the execution of a program as per our requirements. Vectors are part of the C++ Standard Template Library. To use vectors, we need to include the vector header file in our program. The vector class provides various methods to perform different operations on vectors. Add Elements to a Vector: To add a single element into a vector, we use the push_back() function. It inserts an element into the end of the vector. Access Elements of a Vector: In C++, we use the index number to access the vector elements. Here, we use the at() function to access the element from the specified index.

In C++ programming we are using the iostream standard library, it provides cin and cout methods for reading from input and writing to output respectively. To read and write from a file we are using the standard C++ library called fstream. Let us see the data types define in fstream library is: • ofstream: This data type represents the output file stream and is used to create files and to write information to files. • ifstream: This data type represents the input file stream and is used to read information from files. • fstream: This data type represents the file stream generally, and has the capabilities of both ofstream and ifstream which means it can create files, write information to files, and read information from files.

Consider a situation, when we have two persons with the same name, jhon, in the same class. Whenever we need to differentiate them definitely we would have to use some additional information along with their name, like either the area, if they live in different area or their mother's or father's name, etc. Same situation can arise in your C++ applications. For example, you might be writing some code that has a function called xyz() and there is another library available which is also having same function xyz(). Now the compiler has no way of knowing which version of xyz() function you are referring to within your code.

Break statement in C++ is a loop control statement defined using the break keyword. It is used to stop the current execution and proceed with the next one. When a compiler calls the break statement, it immediately stops the execution of the loop and transfers the control outside the loop and executes the other statements. In the case of a nested loop, break the statement stops the execution of the inner loop and proceeds with the outer loop. The statement itself says it breaks the loop. When the break statement is called in the program, it immediately terminates the loop and transfers the flow control to the statement mentioned outside the loop.

A program shall contain a global function named main, which is the designated start of the program in hosted environment. main() function is the entry point of any C++ program. It is the point at which execution of program is started. When a C++ program is executed, the execution control goes directly to the main() function. Every C++ program have a main() function.

Add element at the end. Adds a new element at the end of the vector, after its current last element. The content of val is copied (or moved) to the new element. This effectively increases the container size by one, which causes an automatic reallocation of the allocated storage space if -and only if- the new vector size surpasses the current vector capacity. push_back() function is used to push elements into a vector from the back. The new value is inserted into the vector at the end, after the current last element and the container size is increased by 1. This function does not return any value.

#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program.

Relational operators for vector. Performs the appropriate comparison operation between the vector containers lhs and rhs. In C++, relational and logical operators compare two or more operands and return either true or false values. The equality comparison (operator==) is performed by first comparing sizes, and if they match, the elements are compared sequentially using operator==, stopping at the first mismatch (as if using algorithm equal). The less-than comparison (operator<) behaves as if using algorithm lexicographical_compare, which compares the elements sequentially using operator< in a reciprocal manner (i.e., checking both a<b and b<a) and stopping at the first occurrence.

C supports nesting of loops in C. Nesting of loops is the feature in C that allows the looping of statements inside another loop. Any number of loops can be defined inside another loop, i.e., there is no restriction for defining any number of loops. The nesting level can be defined at n times. You can define any type of loop inside another loop; for example, you can define 'while' loop inside a 'for' loop. A loop inside another loop is called a nested loop. The depth of nested loop depends on the complexity of a problem. We can have any number of nested loops as required. Consider a nested loop where the outer loop runs n times and consists of another loop inside it. The inner loop runs m times. Then, the total number of times the inner loop runs during the program execution is n*m.

In while loop, condition is evaluated first and if it returns true then the statements inside while loop execute, this happens repeatedly until the condition returns false. When condition returns false, the control comes out of loop and jumps to the next statement in the program after while loop. The important point to note when using while loop is that we need to use increment or decrement statement inside while loop so that the loop variable gets changed on each iteration, and at some point condition returns false. This way we can end the execution of while loop otherwise the loop would execute indefinitely. A while loop that never stops is said to be the infinite while loop, when we give the condition in such a way so that it never returns false, then the loops becomes infinite and repeats itself indefinitely.

Logical Operators are used to compare and connect two or more expressions or variables, such that the value of the expression is completely dependent on the original expression or value or variable. We use logical operators to check whether an expression is true or false. If the expression is true, it returns 1 whereas if the expression is false, it returns 0. Assume variable A holds 1 and variable B holds 0:

The cin object is used to accept input from the standard input device i.e. keyboard. It is defined in the iostream header file. C++ cin statement is the instance of the class istream and is used to read input from the standard input device which is usually a keyboard. The extraction operator(>>) is used along with the object cin for reading inputs. The extraction operator extracts the data from the object cin which is entered using the keyboard. The "c" in cin refers to "character" and "in" means "input". Hence cin means "character input". The cin object is used along with the extraction operator >> in order to receive a stream of characters.

Encodes any message using 'RSA Algorithm'. Input is 'Case Sensitive' and works only for all characters. RSA is one of the first practicable 'public-key' cryptosystems and is widely used

This is a C++ Program to perform "dictionary" operations in binary search tree. In computer science, a binary search tree, sometimes also called an ordered or "sorted binary tree", is a