Questions
Please use python: # Problem Description Given a directed graph G = (V, E), find the...

Please use python:

# Problem Description

Given a directed graph G = (V, E), find the number of connected components in G.

# Input

The graph has `n` vertices and `m` edges.
There are m + 1 lines, the first line gives two numbers `n` and `m`, describing the number of vertices and edges. Each of the following lines contains two numbers `a` and `b` meaning there is an edge (a,b) belong to E. All the numbers in a line are separated by space. (`1 <= a,b <= n`)

You can assume that 2 <= n <= 10000, 1 <= m <= 50000.

Your code should read the input from standard input (e.g.
using functions `input()/raw_input()` in Python and `cin/scanf` in C++).

# Output

One number representing the number of connected components in the graph.


Your code should write the output to standard output (e.g. using functions `print` in Python and `cout/printf` in C++).

# Requirement

Your algorithm should run in O(|V| + |E|) time.

Time limtation: 5 seconds.

Memory limitation: 1.0 GB.

# Environment

Your code will be running on Ubuntu 18.04.5.

Now only accept C++ and Python2/Python3 code, g++ version 7.5.0 and Python versions are Python 2.7.17 and Python 3.6.9.

# Examples and Testing

Some examples (e.g., input-x.txt and output-x.txt, x = 1, 2) are provided.
A figure corresponding input-1 can be found in this repo too.
For Python code, try the following to test your code
```
python ./solution.py < input-x.txt > my-output-x.txt
```
For C++ code, try the following to test your code
```
g++ -o mybinary solution.cpp
./mybinary < input-x.txt > my-output-x.txt
```

Your output `my-output-x.txt` needs to be *match exactly* to the given `output-x.txt`.
On Unix-based systems you can use `diff` to compare them:
```
diff my-output-x.txt output-x.txt
```
On Windows you can use `fc` to compare them:
```
fc my-output-x.txt output-x.txt
```

# Submission

If you want to upload a single file, make sure the file is named as `solution.py` (for Python) or `solution.cpp` (for C++).
If you submit via GitHub, make sure your file is located in directory `assignment3/problem1/solution.py` (for Python) or `assignment3/problem1/solution.cpp` (for C++).

# Hints

Use adjacency list to store all the edges.

In: Computer Science

You are asking to develop a “Triangle Guessing” game in Python for the assignment. The program...

You are asking to develop a “Triangle Guessing” game in Python for the assignment. The program accepts the lengths of three (3) sides of a triangle as input from a player. The program output should indicate whether the triangle is a right triangle, an acute triangle, or an obtuse triangle.

1.Tips to help you for this assignment: Make sure each side of the triangle is a positive integer. Use try/except for none positive integer input.

2. Validating the triangle. That is the sum of the lengths of any two sides of a triangle is greater than the length of the third side. Use try/except for invalid sides input.

3. For any wrong input, tell the player what is wrong and asking to re-enter.

4. Allow a player to play multiple times, for example, using a while loop. Remind the player what input to stop the game.

5. Develop your game/program a user friendly one. Document your program by adding comments: Introduce/describe the program including the author, date, goal/purpose of the program Document every variable used at the program even the name is meaningful Explain/comment operation/activity/logic of your code. For example, “Checking whether the inputs are positive integers” and “Checking the validity the 3 lengths to see if they are able to form a triangle”

6. Testing your program for all possible input and every of the 3 possible triangles

In: Computer Science

Too often, statistics are used to ‘prove’ some point or to persuade an audience to some...

Too often, statistics are used to ‘prove’ some point or to persuade an audience to some particular point of view, without really being accurate, complete, or honest. This issue has been the subject of numerous texts. You may be interested in reading such titles as Damned Lies and Statistics, or How to Lie with Statistics. Explain why the use of analytics contributed to the problem. Discuss the consequences of the matter. Did the company/organization involve suffer any adverse consequences?

In: Computer Science

Convert the attached C++ code to working Java code. Be judicious in the change that you...

Convert the attached C++ code to working Java code. Be judicious in the change that you make. This assignment is not about re-writing or improving this code, but rather about recognizing the differences between C++ and Java, and making the necessary coding changes to accommodate how Java does things. PLEASE DO NOT use a built-in Java QUEUE (or any other) container. Additional resources for assignment:

#include <iostream>

#include <string>

using namespace std;

class pizza

{

public:

string ingrediants, address;

pizza *next;

pizza(string ingrediants, string address)

{

this->address = address;

this->ingrediants = ingrediants;

next = NULL;

}

};

void enqueue(pizza **head, pizza **tail, pizza *thispizza)

{

if (*head == NULL) *head = thispizza;

else (*tail)->next = thispizza;

*tail = thispizza;

return;

}

pizza* dequeue(pizza **head, pizza **tail)

{

pizza* pizzatodeliver = NULL;

if (*head != NULL)

{

pizzatodeliver = *head;

*head = (*head)->next;

}

if (*head == NULL) *tail = NULL;

return pizzatodeliver;

}

void deliver(pizza **head, pizza`** tail)

{

pizza *thispizza = dequeue(head, tail);

if (thispizza == NULL)

{

cout << "No deliveries pending" << endl; return;

}

cout << "Deliver a pizza with " << thispizza->ingrediants

<< " to " << thispizza->address << endl;

}

int main()

{

pizza *first = NULL, *last = NULL;

enqueue(&first, &last, new pizza("pepperoni", "1234 Bobcat Trail"));

enqueue(&first, &last, new pizza("sausage", "2345 University Drive"));

deliver(&first, &last);

enqueue(&first, &last, new pizza("extra cheese", "3456 Rickster Road"));

enqueue(&first, &last, new pizza("everything", "4567 King Court"));

enqueue(&first, &last, new pizza("coffee beans", "5678 Java Circle"));

deliver(&first, &last);

deliver(&first, &last);

deliver(&first, &last);

deliver(&first, &last);

deliver(&first, &last);

cin.get();

return 0;

}

In: Computer Science

Write a C program to store the parameters of a circle on a 2D plane: the...

Write a C program to store the parameters of a circle on a 2D plane: the x and y coordinates of the center point and r, the radius. All three parameters are real (floating point) numbers.

-Define a type to store the parameters of a circle. Write a function that receives the parameters of two circles as function parameters, and returns whether the two circles overlap or not. (Two circles overlap if the distance between their center points - computed by the Pythagorean theorem - is less than the sum of their radius.)

-Write a function that asks the user to enter the parameters of a circle, creates the circle, and returns it.

-Expand the type definition and the functions to a program that reads the data of two circles, decides if they overlap, and displays a message accordingly.

In: Computer Science

Accessing applications in the Cloud is easier than traditional applications, as it can be a matter...

Accessing applications in the Cloud is easier than traditional applications, as it can be a matter of just going to a separate website. What are the dangers of this benefit? In a minimum one-page document, discuss this ability and benefit of using Cloud services for applications and where the pitfalls are. Relate your discussion to a specific application type for your comments. Discuss how this may be a benefit or not to a company using the Cloud for its applictions.

In: Computer Science

Use Python programming to find most popular single products and co-purchased products from the large transaction...

Use Python programming to find most popular single products and co-purchased products from the large transaction data: retail.csv. Each row in the file is one purchase transaction from a customer, including a set of product ids separated by commas. The first column is transaction ID, column 2-3 are the products ID purchased in this transaction. It is worth mentioning that if the value in third column is zero, it means this customer only purchased one product (the one in second column).

Note:

• Co-purchased products is defined as a pair of products purchased in the same transaction. For example a row is: "2 24 35". Then 24 and 35 is a pair of copurchased products IDs,.

• To find co-purchased product in each transaction, you might use a nested loop.

• Write top 10 single products and top 10 co-purchased product pairs into a new file: output.txt

In: Computer Science

* Python * * Python Programming * Part 1: Product Class Write a class named Product...

* Python *

* Python Programming *

Part 1: Product Class

Write a class named Product that holds data about an item in a retail store.   The class should store the following data in attributes: product id, item description, units in inventory, and price. Write the __init__ method to require all four attributes. Also write a __str__ method for debugging output.   

Once you have written the class, write a main() function that creates three Product objects and stores the following data in them. Use the __str__ method to confirm the data is stored properly.

ID         Description              Quantity        Price
1          Jacket                          12                    59.95
2          Designer Jeans            40                    34.95
3          Shirt                             20                    24.95

Submit your python file (.py) to Sakai

Part 2: Inventory Module

Create an Inventory module by moving the Product Class definition to another file called inventory.py.   Add three __doc__ strings: one that describe the inventory module (include your name and the date here), one that describes the Product class, and one that describes the Product class __init__ method.

Rename your main() function to test_inventory.py
Add an import at the top of the file to read the Product class definition.
Add these print statements to test the __doc__ string:

Print(inventory.__doc__)

Print(Product.__doc__)

Print(Product.__init__..__doc__)

Test the program to be sure it still works. NOTE: If using IDLE, you will have to explicitly save the inventory.py file after making changes.  

In: Computer Science

FOR JAVA Write a method called findNum that takes a two-dimension array of integers and an...

FOR JAVA

Write a method called findNum that takes a two-dimension array of integers and an int as parameters and returns the number of times the integer parameter appears in the array.

For example, if the array (as created by the program below) is

10 45 3 8

2 42

3 21 44

And the integer parameter is 3, the value returned would be 2 (the number 3 appears two times in the array)

public class HomeworkA {

public static void main(String args[]){

int arr[][] = {{10, 45, 3, 8}, {2, 42}, {3, 21, 44}};

System.out.println(“The number time 3 appears is “+findNums(arr,3)); }

public static int findNum (int [][] myArray) {

..........

In: Computer Science

6) Solving for method Newton-Rapson starting from (1,1,1) in matlab (with algorithm) 1) sin(x) + y^2...

6) Solving for method Newton-Rapson starting from (1,1,1) in matlab (with algorithm)

1) sin(x) + y^2 + ln(z) – 7 = 0

2) 3x + 2^y – z^3 + 1 = 0

3) x + y + z – 5 = 0

In: Computer Science

Write a function named mirror_tree that accepts a pointer to the root of a binary tree...

Write a function named mirror_tree that accepts a pointer to the root of a binary tree of integers. Your function should rearrange the nodes into a mirror tree of the original tree. The mirror tree has the left and right subtrees reversed for each node.

Constraints: You must implement your function recursively and without using loops. Do not construct any new BST objects in solving this problem (though you may create as many NODE* pointer variables as you like). Do not use any auxiliary data structures to solve this problem (no array, vector, stack, queue, string, etc).

Assume that you are using the NODE structure as defined below:

struct NODE {
int Key;
NODE* Left;
NODE* Right;
};

If you run mirror_tree on a BST, is your new tree still a BST?

Code to edit:

/ util.h

#include <iostream>
#include "bst.h"
using namespace std;

// mirror_tree:
//
// Your function should rearrange the nodes into a mirror tree
// of the original tree. The mirror tree has the left and right
// subtrees reversed for each node.
//
void mirror_tree(NODE* node) {
// TO DO: Write this function.
}

In: Computer Science

Find the runtime of this function, where n is an integer. int function(int n) { int...

Find the runtime of this function, where n is an integer.

int function(int n) {
  int a = 0;
  for (int i = n; i > 0; i--) {
    for (int j = 0; j < i; j++) {
      a = a + i + j;
    }
  }
  return a;
}

Find the runtime of this function, where m and n are two integers.

int f(int m, int n) {
  if (m==1 || n==1) {
    return 1;
  }
  return f(m, n-1) + f(m-1, n);
}

Please explain to me each of the steps and how you receive the answer.

Thank you

In: Computer Science

Please include all inputs and outputs in details with pictures: General rules: Create homework, compose specifications...

Please include all inputs and outputs in details with pictures:

General rules: Create homework, compose specifications or any text by using a common document-creation tool, such as Microsoft® Word.

Detailed Hints: Refer to the wwweb or lecture notes for this class to design, implement, and debug solid SW solutions. Be concise, complete, and precise.

Abstract: Compute the performance data for the schedulers of three types of Operating Systems. Do not get scared! Only the timing for each scheduler is of interest. You can compute these timing data by hand or by actually implementing a simulator. Either solution is feasible and permitted. The simulator measures key performance data, such as throughput, wait time, and turn-around time. You may also manually compute these numbers without having to run them in a simulation environment.

If you chose to “manually” compute the data, i.e. without implementing a SW simulator, the amount of “generated performance data” is allowed to be way less detailed than what is suggested here.

One OS is a strict batch system with a non-preemptive First Come First Serve (FCFS) scheduler. The second OS uses a non-preemptive, high-priority first (HPF) scheduler, while the third OS uses a preemptive round robin (RR) scheduler with a variable time-quantum with varying context switch overhead. Design meaningful input data, run them through all schedulers, generate output data, and interpret and discuss the results. To start your simulations, use the sample data from this HW assignment. In addition, provide 2 additional, meaningful input scenarios, more complex than the samples given here.

Detail: HomeWork 1 consists of the following parts with equal weight each:

  • Compute performance data for the scheduler of a non-preemptive FCFS batch system
  • Ditto for a non-preemptive HPF batch system, and
  • Ditto for a preemptive RR scheduler; you’ll vary time quantum and overhead.
  • Invent meaningful input data; run them through your schedulers to produce output
  • Discuss the generated data. Hand in your discussion in typed form

Input: Input to the schedulers is a list of processes, for which you happen to know the execution time in milliseconds. For your program input, each process is represented by a triple of decimal numbers id, time, priority. These multiple processes are scheduled and compete for the CPU resource. Here id is the name of the process; time is the time in milliseconds that process id needs to run to completion, and priority is the priority of process id. A plausible input sample for two processes with process id 1 and process id 4 is:

1          2          3

4          50        6

Depending on the scheduler, priority may be ignored. The meaning of triples is as follows:

1   2 3          process 1 uses 2 milliseconds to run, has priority 3

4 50 6           process 4 uses 50 milliseconds, has priority 6, with 0 being the highest

Use the definitions below to compute for each process Throughput, Wait Time, and Turn-around Time. Compute these for each of the 3 schedulers; also compute the average for all processes.

For the preemptive RR scheduler, vary the time quantum q from 1 to 5 milliseconds (ms). Also, for each q selected, vary the overhead o of a context switch from 0 ms up to q itself. There is no need to vary the o beyond q. When a process scheduled by the RR system has received all time needed to completion, do not add a final o unit in the computation of the total time for that process. Also the first time around, act as if the initial schedule overhead o is 0. Use the sample outputs below as a guide for the detail you should generate for this HW.

Definitions:

Throughput:                    Number of jobs (processes) completed per time unit

Waiting Time:                  The total time a process is in Ready Queue

Average Waiting Time:    Average Waiting Time of n processes is: total waiting time by n

Turn-around Time:          span of time of submission to completion time

       

Example 1:

Enter triples: process id, time in ms, and priority:

For example:

1 12 0

3 9 1

2 99 9

process 1 needs 12 ms and has priority 0, very high,

process 3 needs 9 ms and has priority 1.

and so on ...

1 2 3

2 1 2

Process list in FCFS order as entered:

1 2 3

2 1 2

End of list.

fcfs wait of p1 = 0

fcfs wait of p2 = 2

average wait for 2 procs = 1

fcfs turn-around time for p1 = 2

fcfs turn-around time for p2 = 3

average turn-around for 2 procs = 2.5

fcfs throughput for 2 procs = 0.666667 proc/ms

<><> end FCFS <><>

Process list in HPF order:

2 1 2

1 2 3

End of list.

hpf wait of p2 = 0

hpf wait of p1 = 1

average wait time for 2 procs = 0.5

hpf turn-around for p2 = 1

hpf turn-around for p1 = 3

average turn-around for 2 procs = 2

hpf throughput for 2 procs = 0.666667 proc/ms

<><> end HPF schedule <><>

Process list for RR in order entered:

1 2 3

2 1 2

End of list.

preemptive RR schedule, quantum = 1 overhead = 0

RR TA time for finished p2 = 2, needed: 1 ms, and: 1 time slices.

RR TA time for finished p1 = 3, needed: 2 ms, and: 2 time slices.

RR Throughput, 2 p, with q: 1, o: 0, is: 0.666667 p/ms, or 666.667 p/us

Average RR TA, 2 p, with q: 1, o: 0, is: 2.5

preemptive RR schedule, quantum = 1 overhead = 1

RR TA time for finished p2 = 3, needed: 1 ms, and: 1 time slices.

RR TA time for finished p1 = 5, needed: 2 ms, and: 2 time slices.

RR Throughput, 2 p, with q: 1, o: 1, is: 0.4 p/ms, or 400 p/us

Average RR TA, 2 p, with q: 1, o: 1, is: 4

preemptive RR schedule, quantum = 2 overhead = 0

RR TA time for finished p1 = 2, needed: 2 ms, and: 1 time slices.

RR TA time for finished p2 = 3, needed: 1 ms, and: 1 time slices.

RR Throughput, 2 p, with q: 2, o: 0, is: 0.666667 p/ms, or 666.667 p/us

Average RR TA, 2 p, with q: 2, o: 0, is: 2.5

preemptive RR schedule, quantum = 2 overhead = 1

RR TA time for finished p1 = 2, needed: 2 ms, and: 1 time slices.

RR TA time for finished p2 = 4, needed: 1 ms, and: 1 time slices.

RR Throughput, 2 p, with q: 2, o: 1, is: 0.5 p/ms, or 500 p/us

Average RR TA, 2 p, with q: 2, o: 1, is: 3

preemptive RR schedule, quantum = 2 overhead = 2

RR TA time for finished p1 = 2, needed: 2 ms, and: 1 time slices.

RR TA time for finished p2 = 5, needed: 1 ms, and: 1 time slices.

RR Throughput, 2 p, with q: 2, o: 2, is: 0.4 p/ms, or 400 p/us

Average RR TA, 2 p, with q: 2, o: 2, is: 3.5

<><> end preemptive RR schedule <><>

Example 2:

Enter triples: process id, time in ms, and priority:

For example:

1 12 0

3 9 1

2 99 9

process 1 needs 12 ms and has priority 0, very high,

process 3 needs 9 ms and has priority 1.

and so on ...

1 10 5

2 8 1

3 12 7

Process list in FCFS order as entered:

1 10 5

2 8 1

3 12 7

End of list.

fcfs wait of p1 = 0

fcfs wait of p2 = 10

fcfs wait of p3 = 18

average wait for 3 procs = 9.33333

fcfs turn-around time for p1 = 10

fcfs turn-around time for p2 = 18

fcfs turn-around time for p3 = 30

average turn-around for 3 procs = 19.3333

fcfs throughput for 3 procs = 0.1 proc/ms

<><> end FCFS <><>

Process list in HPF order:

2 8 1

1 10 5

3 12 7

End of list.

hpf wait of p2 = 0

hpf wait of p1 = 8

hpf wait of p3 = 18

average wait time for 3 procs = 8.66667

hpf turn-around for p2 = 8

hpf turn-around for p1 = 18

hpf turn-around for p3 = 30

average turn-around for 3 procs = 18.6667

hpf throughput for 3 procs = 0.1 proc/ms

<><> end HPF schedule <><>

Process list for RR in order entered:

1 10 5

2 8 1

3 12 7

End of list.

preemptive RR schedule, quantum = 1 overhead = 0

RR TA time for finished p2 = 23, needed: 8 ms, and: 8 time slices.

RR TA time for finished p1 = 27, needed: 10 ms, and: 10 time slices.

RR TA time for finished p3 = 30, needed: 12 ms, and: 12 time slices.

RR Throughput, 3 p, with q: 1, o: 0, is: 0.1 p/ms, or 100 p/us

Average RR TA, 3 p, with q: 1, o: 0, is: 26.6667

preemptive RR schedule, quantum = 1 overhead = 1

RR TA time for finished p2 = 45, needed: 8 ms, and: 8 time slices.

RR TA time for finished p1 = 53, needed: 10 ms, and: 10 time slices.

RR TA time for finished p3 = 59, needed: 12 ms, and: 12 time slices.

RR Throughput, 3 p, with q: 1, o: 1, is: 0.0508475 p/ms, or 50.8475 p/us

Average RR TA, 3 p, with q: 1, o: 1, is: 52.3333

preemptive RR schedule, quantum = 2 overhead = 0

RR TA time for finished p2 = 22, needed: 8 ms, and: 4 time slices.

RR TA time for finished p1 = 26, needed: 10 ms, and: 5 time slices.

RR TA time for finished p3 = 30, needed: 12 ms, and: 6 time slices.

RR Throughput, 3 p, with q: 2, o: 0, is: 0.1 p/ms, or 100 p/us

Average RR TA, 3 p, with q: 2, o: 0, is: 26

preemptive RR schedule, quantum = 2 overhead = 1

RR TA time for finished p2 = 32, needed: 8 ms, and: 4 time slices.

RR TA time for finished p1 = 38, needed: 10 ms, and: 5 time slices.

RR TA time for finished p3 = 44, needed: 12 ms, and: 6 time slices.

RR Throughput, 3 p, with q: 2, o: 1, is: 0.0681818 p/ms, or 68.1818 p/us

Average RR TA, 3 p, with q: 2, o: 1, is: 38

preemptive RR schedule, quantum = 2 overhead = 2

RR TA time for finished p2 = 42, needed: 8 ms, and: 4 time slices.

RR TA time for finished p1 = 50, needed: 10 ms, and: 5 time slices.

RR TA time for finished p3 = 58, needed: 12 ms, and: 6 time slices.

RR Throughput, 3 p, with q: 2, o: 2, is: 0.0517241 p/ms, or 51.7241 p/us

Average RR TA, 3 p, with q: 2, o: 2, is: 50

preemptive RR schedule, quantum = 3 overhead = 0

RR TA time for finished p2 = 23, needed: 8 ms, and: 3 time slices.

RR TA time for finished p1 = 27, needed: 10 ms, and: 4 time slices.

RR TA time for finished p3 = 30, needed: 12 ms, and: 4 time slices.

RR Throughput, 3 p, with q: 3, o: 0, is: 0.1 p/ms, or 100 p/us

Average RR TA, 3 p, with q: 3, o: 0, is: 26.6667

preemptive RR schedule, quantum = 3 overhead = 1

RR TA time for finished p2 = 30, needed: 8 ms, and: 3 time slices.

RR TA time for finished p1 = 36, needed: 10 ms, and: 4 time slices.

RR TA time for finished p3 = 40, needed: 12 ms, and: 4 time slices.

RR Throughput, 3 p, with q: 3, o: 1, is: 0.075 p/ms, or 75 p/us

Average RR TA, 3 p, with q: 3, o: 1, is: 35.3333

preemptive RR schedule, quantum = 4 overhead = 0

RR TA time for finished p2 = 20, needed: 8 ms, and: 2 time slices.

RR TA time for finished p1 = 26, needed: 10 ms, and: 3 time slices.

RR TA time for finished p3 = 30, needed: 12 ms, and: 3 time slices.

RR Throughput, 3 p, with q: 4, o: 0, is: 0.1 p/ms, or 100 p/us

Average RR TA, 3 p, with q: 4, o: 0, is: 25.3333

preemptive RR schedule, quantum = 5 overhead = 0

RR TA time for finished p1 = 20, needed: 10 ms, and: 2 time slices.

RR TA time for finished p2 = 23, needed: 8 ms, and: 2 time slices.

RR TA time for finished p3 = 30, needed: 12 ms, and: 3 time slices.

RR Throughput, 3 p, with q: 5, o: 0, is: 0.1 p/ms, or 100 p/us

Average RR TA, 3 p, with q: 5, o: 0, is: 24.3333

some outputs I have to cut because Chegg say the question is long

In: Computer Science

{ /* Write a program in C++ to find the perfect numbers between 1 and 500....

{
/* Write a program in C++ to find the perfect numbers between 1 and 500.
The perfect numbers between 1 to 500 are:
6
28
496*/
   int sum = 0;
   int i = 1;
   int j = 1;
   for (i ; i <= 100; i++)
   {
       for (j; j <= 100; j++)
       {
           if (j < i)//Line 55
           {
              
           if (i % j == 0)
               sum = sum + j;
           }
          
       }

       if (sum == i)
      
           cout << i << " ";
       j = 1;//line 66
       sum = 0;
      
   }
  
   return 0;
}

Can someone please explain the code above and how it works. what is the purpose of the comparison on line 55 and I on line 66?

In: Computer Science

1. Based upon the pseudocode given in “Background”, solve the three versions of add up to...

1. Based upon the pseudocode given in “Background”, solve the three versions of add up to K problems: ◦ all pairs of numbers that add up to K ◦ all triples (set of three) that add up to K ◦ all possible subsets of numbers that add up to K: recursive version is given, you are asked to solve it iteratively. 2. Test the three functions using given test cases. 3. Measure the running time of algorithms under various input sizes.

#include <iostream>
#include <bitset>
#include <vector>
#include <time.h>
using namespace std;

const double BILLION = 1E9;

 /* Print ALL pairs of number from the given array a[0...a_len-1] that adds up to K 
    @param intList: the list of int values 
    @param K: the sum we want to make
   */
void PairsAddupToK (int a[], int a_len,  int K)
{
 //TODO by you
}
   
/* Print ALL triples of numbers from the given array a[0...a_len-1] that adds up to K 
@param intList: the list of int values 
@param K: the sum we want to make
*/
void TriplesAddupToK (int a[], int a_len, int K)
{
        //todo by you
}
   
/* Print ALL subsets of numbers from the given array, a[0...a_len-1 that adds up to K 
this function solves the problem iteratively 
NOTE: WE assume a_len<32 (so that we can use int (which uses 32 or 64 bits) 
  as code to decide subset  
@param intList: the list of int values 
@param K: the sum we want to make
*/
void SubsetsAddupToK (int a[], int a_len, int K)
{
        //todo by you
}

            
        
//Recursive solution to SubsetAddupToK 
/* This functions tries to use numbers from a[left...right] to make sum 
 @param a[] array of int
 @n: a[left...right] is the list of numbers to use
 @currentSum: current sum we have made using numbers in numsUsed
 @finalSum: final sum that we need to make
 @numsUsed: numbers that are used to get us here */
bool SubsetsAddupToK (int  a[], int left, int right, int currentSum, int finalSum, vector<int> numsUsed)
{
        //Three base cases: 
        //case 1: what remains to make is negative: we have overshot... 
        if (currentSum>finalSum)
                return false; 

        //case 2: we made it! 
        // Result is only displayed at the base case. 
        if (currentSum==finalSum)
        {
                cout <<"Solution:";
                for (int i=0;i<numsUsed.size();i++)
                        cout <<numsUsed[i]<<",";
                cout <<endl;
                return true;
        }

        //case 3: we don't have any number to use: fails. 
        if (left>right && currentSum < finalSum)
                return false;


        //General calse: what remains to make is positive, and we have numbers to use 
        bool res1=false, res2=false;

        // Explore both possibilities: Use a[left] or not 
        //  1) include a[left] in the sum, i.e., make sum-a[left] using a[left+1...right]  
        vector<int> newNums = numsUsed;
        newNums.push_back (a[left]); //now a[left] is used ... 

        res1 = SubsetsAddupToK(a, left+1, right, currentSum+a[left], finalSum,newNums);
            // here the currentSum is increased by a[left], a[left] is added into list of 
            // numbers used... 

        //  2) do not include a[left] in the sum, i.e., make sum using a[left+1...right]  
        //     currentSum, finalSum, numsUsed is not changed 
        res2 = SubsetsAddupToK (a, left+1, right, currentSum, finalSum, numsUsed);
                                    //^+1 so that a[left] is not considered any more

        //if either of the above two succeeded, return true 
        if (res1 || res2)
                return true;
        else
                return false;
}


/* Compare the running time of three functions for input size n 
 @param n: the length of vecotr/list
 */ 
void  MeasureRunningTime (int a[], int a_len)
{
        struct timespec start, finish;
        double r1, r2, r3, r4;


           clock_gettime (CLOCK_REALTIME, &start);
           PairsAddupToK (a, a_len, 100);
           clock_gettime (CLOCK_REALTIME, &finish);
           r1 = (finish.tv_sec - start.tv_sec)+
                        (finish.tv_nsec-start.tv_nsec)/BILLION;


           clock_gettime (CLOCK_REALTIME, &start);
           TriplesAddupToK (a, a_len, 100);
           clock_gettime (CLOCK_REALTIME, &finish);
           r2 = (finish.tv_sec - start.tv_sec)+
                        (finish.tv_nsec-start.tv_nsec)/BILLION;

           clock_gettime (CLOCK_REALTIME, &start);
           SubsetsAddupToK (a, a_len, 100); 
           clock_gettime (CLOCK_REALTIME, &finish);
           r3 = (finish.tv_sec - start.tv_sec)+
                        (finish.tv_nsec-start.tv_nsec)/BILLION;

           clock_gettime (CLOCK_REALTIME, &start);
           vector<int> results;
           SubsetsAddupToK (a, 0, a_len, 0, 100, results); 
                                        //^ current sum=0, final sum=100
           clock_gettime (CLOCK_REALTIME, &finish);
           r4 = (finish.tv_sec - start.tv_sec)+
                        (finish.tv_nsec-start.tv_nsec)/BILLION;

           cout <<"n="<<a_len<<"\t";
           cout <<"Pairs"<<"\t"<< r1<<"s\t";
           cout <<"Triples"<<"\t"<< r2<<"s\t";
           cout <<"Subsets"<<"\t"<< r3<<"s\t";
           cout <<"SubsetsR"<<"\t"<< r4 <<"s\t"<<endl;
}

int main()
{
        vector<int> numsUsed; //this is empty initially 

        int a5[5] = {50,90,20,30,10};
        int a10[10] = {50, 33, 90, 2, 20, 
                72, 30,10, 92, 8};
        int a20[20] = {50, 33, 11, 79, 90,
                2, 20, 72, 30,10, 
                92, 8, 99, 101, 25,
                71, 48, 72, 35, 9};
        int a30[30] = {50, 33, 11, 79, 90, 
                2, 20, 72, 30,10, 
                92, 8, 99, 101, 25, 
                71, 48, 72, 35, 9,
                37, 41, 55, 73, 67,
                66, 22, 11, 6, 4};

        //Testing 
        //Display all different ways to make 100 using a5[0...4]
        int SumToMake = 100;
        int curSum=0; 
        SubsetsAddupToK (a5, 0, 4, curSum, SumToMake, numsUsed);

        /* Measuring and comparing running time  
        MeasureRunningTime (a5, 5);
        MeasureRunningTime (a10, 10);
        MeasureRunningTime (a20, 20); */
        MeasureRunningTime (a30, 30);
}

In: Computer Science