An example of connected components in an undirected graph - graph

I am doing an algorithm problem about finding connected components in an undirected graph.
The input is a list of edges stores in (source target), and the output is (vertex,label).
source is an integer representing id of source vertex,
target is an integer representing id of target vertex,
vertex is vertex id,
label is the label of a connected component to which vertex belongs.
Here are two examples:
Example 1.
input:
0 1
1 2
3 1
output:
(0, 0)
(1, 0)
(2, 0)
Example 2.
input:
0 3
4 5
output:
(3, 0)
(4, 4)
(5, 4)
In my understanding, for the 1st example, all vertex connect to 1, so the graph has one connected components, so the result should be
(0,0)
(1,0)
(2,0)
(3,0)
but the answer does not have (3,0).
For the 2nd example, 0 connect to 3, 4 connect to 5, there are two connected components. The result should be
(0,0)
(3,0)
(4,4)
(5,4)
but the answer does not have (0,0)
I am not sure do I misunderstanding something that the result is unequal to the output it gives..

Come answer my silly question again... The main point is those two input is a same graph. And the label should be the smallest vertex in the component.

Related

Find the maximum number of distinct graphs

The question is: How many distinct non-oriented graphs, with 6 nodes, numbered from 1 to 6, can be constructed so that node 2 has degree 1. Two graphs are distinct if their adjacency matrices are different.
I know some notions about graphs and I thought that I would have to find out the number of distinct non-oriented graphs that can be built with 5 nodes and then multiply that by the number 5.
The problem is that I can't figure it out because I don't really understand the notion of degree in a graph.
I would appreciate your help. Thanks!!
Taking into consideration again, some aspects of this problem, I have reached a conclusion.
There are 5 nodes that can form the distinct graphs, because 1, 3, 4, 5, 6 are, possibly, the adjacent nodes of node 2.
With these five nodes, there can be 2 ^ 10 distinct unoriented graphs, which is 2 ^ (5*2).The logic is that 5 is the number of nodes that are adjacent to node 2, and 2 is the number of connections that each node has, because the graph is undirected. And this number represents the graphs that can be built, in order to respect the condition.
At the end, the only operation to be done is to multiply (2 ^ 10) * 5.
So all the graphs should have 6 nodes, there is no difference between the number of nodes in the number of graphs that are possible.
The notion of symmetry of a graph usually refers to a graph that reflects the behaviour of a mathematical function; in this context, of mathematical functions, it is True that a graph can be symmetric with respect to a line.
https://www.maplesoft.com/support/help/maple/view.aspx?path=MathApps/SymmetriesOfAGraph

All possible possible directed graphs in V={1,2,3}

I got this exercise :
Give an example of three different directed graphs on V = {1, 2, 3}
where the edges (1, 2) and (1, 3) exist. Write both the adjacency-list and adjacency-
matrix representation for each.
I find only this two:
G = {(1,2), (1,3)}
G = {(1,2), (1,3), (2,3)}
What I miss? Something like that is valid: G = {(1,2), (1,3), (3,2)} ?
It's a directed graph, which means all links are one-way. If you want to be able to go from 1 to 2 and from 2 to 1, you need two links, (1,2) and (2,1) - that's part of the definition of a directed graph.
With that, enumerate all possible links for a graph of 3 vertexes:
(1,2)
(2,1)
(1,3)
...
Once you enumerate all possible links in such a graph, you can pick and choose unique sets of those links to make into multiple graphs, subject to the constraints given to you by the exercise.
For instance, here are a couple graphs:
{(1,2)}
{(2,1)}
{(1,3)}
{(3,1)}
{(3,2), (2,1), (1,3)}
...
You already have two out of three requested answers and you need a third graph to complete the exercise. You need to give answers that include two provided links. Why not give as an answer a graph that has every link in it? A graph of every link must contain the two requested links, right?

How to find polygons in a given set of points and edges?

Consider the following problem:
Given N points in plane and M line segments connecting them, find all polygons (convex or concave) that do not contain any other polygons inside.
For instance:
There are 5 polygons founded:
1 - 2 - 5 - 6
2 - 3 - 5
3 - 4 - 5
7 - 8 - 9
10 - 13 - 20 - 12 - 11
How can I identify these polygons and there corresponding vertices and edges? And what is the fastest solution for this?
Build graph with segment ends as vertices and segments as edges, then find cycles using DFS.
Note that the same edges might belong to multiple cycles (polygons) like your 2-5 and there might be many variants to select cycles. To exclude ambiguity, you can build fundamental set of cycles
Edit. As weston noticed in comments, resulted polygons might contains others. So sketch of more geometric approach:
Build lists of adjacency for graph.
Sort edges in ever list by polar angle.
Choose the most bottom vertex A.
If it's degree is 0, remove vertex, if 1, remove vertex and edge.
Otherwise get edge E with the smallest angle from this vertex.
Walk to pair vertex B.
Choose the most left edge F from B.
Move along to F edge into C.
If dead end, remove F and vertex C and return to B.
Repeat moving using left-hand rule until meet vertex A or dead end vertex.
In the walk process remove edges outgoing from vertices of degree 2 or mark them as used.

Modified Shortest path algorithm, forbidden paths

We are provided with a graph G = (V, E), where each edge is associated with some positive weight (W[i] > 0). We are asked to find the shortest path from A to B provided we follow the following conditions :
We are also given some forbidden paths, say x. The goal is the shortest path should not contain any forbidden path as its sub-path.
For example: Consider a graph G with 4 vertices and 5 edges (1, 2, 1), (2, 1, 1), (1, 4, 5), (1, 3, 1.5), (3, 4, 1). Here, the 3rd entity in the bracket denotes the weight of edge between 'u' and 'v'. We are required to find the shortest path between 1 and 4, and the list of forbidden paths contains only the path 1->3->4.
The paths from 1 to 4 are 1 -> 4, 1 -> 2 -> 3 -> 4, 1 -> 3 -> 4.
Out of these, only 1st 2 are valid paths, and among them the shortest path is 1 -> 2 -> 3 -> 4 of total weight as 3.0.
Is there an efficient algorithm for the above task? Please describe the algorithm with the complexity analysis. I would be highly thankful if you could provide a code as well.
You can preprocess the graph in such a way to embed forbidden paths in-to it. For every forbidden path you duplicate vertices which belong to it and then drop some of the edges. That duplicates would have special meaning: walking along duplicated edge would mean that you have come to a vertex along the forbidden path and you can't walk along the last edge of it. If you are walking along original edges, then you have come somewhere to the middle of forbidden path so it does not affect you. To achieve that you drop all incoming edges to a duplicate path excepting edge to it's second vertex from it's first vertex. But you drop that edge from original path.
a forYou split vertices which are part of forbidden paths in-to several virtual vertices and drop some of the edges. Let's suppose that in following graph path ABC is forbidden:
A-->B-->C
D->/
Then you split B to BA and BD (depending from which vertex you have come to B) and drop BA->C edge.
A->BA /->C
D->BD-/
Now you can use classic dijkstra on that preprocessed graph.
More complex example, let's suppose that ABCF is forbidden:
G->\
A-->B-->C-->F
D->/ \->E
So we duplicate B and C as internal vertices of the forbidden route, we drop A->B edge and leave only A->B'. We also drop all other incoming edges to B' and C' but we leave B'->E edge because it drives away from forbidden route. We also drop C'->F edge.
/->B'-->C'
| \------->\
| \
| G->\ \
A B-->C---->F \
D->/ \----------->E

Get node descendants in a tree graph

I have a directed graph (grafopri1fase1) the graph has no loops and it has a tree structure (not binary tree).
I have an array of nodes (meterdiretti) that i have extracted from the graph (grafopri1fase1) matching a condition.
I would like to know starting from each node of Meterdiretti how many nodes are under each node of Meterdiretti.
The result I would like to have is a Matrix with the following format
first column------------ second column
meterdiretti[1] -------- total amount of nodes reachable starting from meterdiretti[1]
meterdiretti[2] -------- total amount of nodes reachable starting from meterdiretti[2]
....
meterdiretti[n] ----------total amount of nodes reachable starting from meterdiretti[n]
Take a punt at what you want - it would be good if you could add a reproducible example to your question.
I think what you want is to count the descendents of a node. You can do this with neighborhood.size and mode="out" argument.
library(igraph)
# create a random graph
g <- graph.tree(17, children = 2)
plot(g, layout=layout.reingold.tilford)
# test on a single node
neighborhood.size( g, vcount(g), "1", "out") - 1
# [1] 16
# apply over a few nodes
neighborhood.size( g, vcount(g), c(1,4,7), "out") - 1
[1] 16 4 2

Resources