Showing posts with label Algorithms. Show all posts
Showing posts with label Algorithms. Show all posts

[Problem] Dinner with GF

My girl friend is interested to buy some cities. Inorder to buy two cities, she needs to buy the road connecting those two cities. She is concentrating on only the roads not cities as she has lot of other work.

My bad luck,  same time I invited her for dinner at Greenpark.

She said invitation will be accepted when I give number of cities count which she bought.

Now, I was given a list of roads, bought by my GF.  I need to tell her how many cities did she buy.

Input:
First line contains an integer T, denoting the number of test cases. The first line of each test case contains an integer E, denoting the number of roads. The next E lines contain two space separated integers X and Y, denoting that there is an road between city X and city Y.

Output:
For each test case, I need to print the number of cities she bought.

Constraint:
1 <= T <= 100
1 <= E <= 1000
1 <= X, Y <= 10000

Let me solve this with Graph representation.
 Key points: Each city will be vertex of the graph.
                    Road will be the edge between the vertex.
   

[Problem]Coffee treat with crush

I recently learnt about Graphs and  very excited!

I went over to the Graph-making factory to watch some freshly prepared graphs. Incidentally, I have seen  beautiful girl who is making nice graphs.
so I decided to step in and invite her for a coffee.
But she gave me one of her problem and said if I solve that she will come with me for a coffee :-(.
My Job is to Identify whether the incoming graph is a tree or not. I was given given N, the number of vertices in the graph and the degree of each vertex.

Find if the graph is a tree or not.

Input:
First line contains an integer N, the number of vertices.
Second line contains N space-separated integers, the degrees of the N vertices.

Output:
Print "Yes" (without the quotes) if the graph is a tree or "No" (without the quotes) otherwise.

Constraints:
1 ≤ N ≤ 100
1 ≤ Degree ≤ 1000

Graph representation


You can represent a graph in many ways.
Graph terminology:
 Vertices: From below graph 1, 2, 3, 4 are Vertices.
 Edges: (1,2), (2,3)..etc are edges.
 Degree: Number of edges from the vertex.
              From below graph  vertex 1 degree is 2 because it had two edges which are(1,2) (1,4).
  Order: Number of vertices will be graph order.
  Size: Number of Edges will be graph size.
Note: The Sum of the degrees is twice the number of edges.

Spanning subgraph: A graph obtained only by edge deletion(G\E).
(i.e Original graph and subgraph number of vertices should be same)

Induced subgraph: A graph obtained only by vertex deletion(G-V)

Edge Existence:

Source: https://www.hackerearth.com/
You have been given an undirected graph consisting of N nodes and M edges. This graph can consist of self-loops as well as multiple edges. In addition , you have also been given Q queries. For each query , you shall be given 2 integers A and B. You just need to find if there exists an edge between node A and node B
. If yes, print "YES" (without quotes) else , print "NO"(without quotes).
Input Format:
The first line consist of 2 integers N
and M denoting the number of nodes and edges respectively. Each of the next M lines consist of 2 integers A and B denoting an undirected edge between node A and B. The next line contains a single integer Q denoting the number of queries. The next Line contains 2 integers A and B denoting the details of the query.
Output Format
Print Q lines, the answer to each query on a new line.
Constraints:
1≤N≤103
1≤M≤103
1≤A,B≤N
1≤Q≤103

The two most common ways of representing a graph is as follows:

Adjacency matrix:
It needs lot of space because if there is no edge between the vertices's it will store the value as zero.

The adjacency matrix of the following graph is:

i/j : 1 2 3 4
1 :   0 1 0 1
2 :   1 0 1 0
3 :   0 1 0 1
4 :   1 0 1 0


Note: If graph has some weight we can represent in the matrix with respective weight instead of 0's and 1's.

In real time scenarios this weight could be distance between two cities.
If you take network as example then  file transfer capacity between the two system could be the graph weight.

below code will illustrate the edge representation with adjacent matrix.