Find length of longest trail in directed unweighted graph - graph

I have a directed, unweighted, possibly cyclic graph that can contain loops and multiple duplicate edges (i.e. two edges from node 1 to node 2).
I would now like to find the length of the longest trail in this graph, i.e. the longest path that:
- uses no edge twice (but if there are multiple edges from node 1 to node 2, it can use every one of them)
- possibly visits nodes several time (i.e. it does not have to be a simple path)
In particular, is this problem NP-hard? I know that the longest simple path is NP-hard (reducing Hamiltonian Path to it) and the longest trail with edge reusal is in P (Bellman ford with weight -1 on every edge). However, with this problem, I am not quite sure and I could not find good information on it.

Although I am not completely sure, I think that this problem is NP-hard. As I understand, your question arises due to multiple edges between nodes. The graphs that has multiple edges between same nodes can be expanded to larger graphs with no multiple edges between them. Thus, a graph with multiple edges between same nodes has no difference than a graph without multiple edges.
Let me walkthrough a simple example to explain:
Let there be a graph with 3 nodes (A,B,C) and 5 edges between them (A to B, A to B, B to A, B to C, C to A)
This graph can be expanded and shown with 5 nodes and 7 edges.
Lets expand the node A to 3 different nodes (A1, A2, A3). When we adjust the edges according to previous edges, there exists 7 edges(A1 to B, A2 to B, B to A3, B to C, C to A1, C to A2, C to A3)
As a result, now we have a graph without multiple edges and can be evaluated with the help of Hamiltonian and Bellman Ford.
Hope I've at least cleared the problem a bit.

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.

Search common subgraph between two graphs

I have two graphs G, H labeled and I want to extract all common subgraph of two graphs, I got to a part that is:
1 - extract all the nodes that are in common, but I'm stuck on the part that includes:
2 - Step 1: Take the First vertex and store it in a set P = {first element} (which will be the set of all common subgraph), and go to 2nd if it is adjacent to the first of the two P graph G and H, we add it, and so on, but I do not know how to do it when i have more than 2
That is a NP-complete problem. See http://en.wikipedia.org/wiki/Subgraph_isomorphism_problem

Partitioning adjacency matrix of bipartite graph

Lets say I have a graph G with its adjacency matrix A. I know that G is bipartite.
How can I split the vertices in G into the two sets that always form a bipartite graph?
Thanks!
Declare an array which of size equal to the number of vertices, setting each element to 0 initially. Then perform a depth-first search through the graph, recording the "level number" that you are on as you go. This starts at 1, and alternates between 1 and 2 with each edge traversed. For every vertex reached, assign the current level to the corresponding entry of which, and (if it was previously 0) recurse to process its children. Afterwards, all elements of which will be either 1 or 2, and which[i] indicates which set vertex i belongs to.
Intuitively, you can imagine that each traversal from parent to child in the DFS takes you "down" a level, and each traversal back takes you back "up". By the bipartite property, all vertices on even levels can be connected only to vertices on odd levels and vice versa, so labelling nodes "even" or "odd" suffices to partition them into the two sets.
If your graph contains more than one component, you will of course need a separate DFS for each component.

Resources