0
$\begingroup$

Assume we have Graphs $G$ and $H$ and $V(G) = V(H)$, where $V$ is a set of vertices. What would be the algorithm to find out if these graphs are identical? The time boundary for this task is $\mathcal{O}(m+n)$ ($m$ and $n$ are not specified but I assume $m$ is $|E(H)|$ and $n$ is $|V(H)|$ where $E$ is a set of edges).

Graphs are not directed nor edge-weighted.

In my opinion the time boundary says us that the algorithm should be based on $DFS$ or $BFS$. In my solution I use $DFS$ to count vertex degree and check if vertex degree in graph $G$ is the same as this vertex degree in $H$. I'm not sure if this is the correct way of solving the problem, thus I am asking you - is it?

EDIT: Ok, so there seems to be a lot of confusion as what 'identical' graph means... So I was not entirely sure at the time I asked the question, but now I know that I need to just check if the sets of edges of these graphs are equal. I still don't know how to do that in $\mathcal{O}(m+n)$ time though (assuming that graphs are stored as adjacency lists).

  • 1
    It depends on how the graph is saved, can you calculate the degree of a vertex fast? Or calculate the number of edges in both graphs fast?2017-01-03
  • 0
    I think that you also need to take into account $|E(G)|$, in some settings.2017-01-03
  • 0
    @JorgeFernándezHidalgo It's not specified but let's assume that we store the graphs as adjacency lists.2017-01-03
  • 1
    Then you're going to have to check each and every edge.2017-01-03
  • 0
    @Ian So ideal representation of these graphs would be lists od edges?2017-01-03
  • 1
    Can you define precisely what you mean by "identical?" The notion of "sameness" for graphs is isomorphism, which is not known to be polynomial time solvable.2017-01-03
  • 0
    @ml0105 is right - the notion of "sameness" here and the precise formulation of the problem is important. I posted an answer that I hope helps. As far as I know, the complexity of graph isomorphism is still an active area of research. Babai showed last year that it can be solved in quasipolynomial time. I provided a link in my answer. Subgraph isomorphism is NP-Complete though.2017-01-03
  • 0
    @Octohorp to answer your question about how to store these graphs, I think adjacency lists would be best because that would be a faster way to do DFS than an edge list. Is there a particular application that you are using this for?2017-01-03
  • 0
    I get the sense that "identical" here means "having precisely the same labeled edges". In this case, it seems one could just check the edges one by one in $O(m)$ time. In any event, relying on degree sequences won't work, as Chris Harshaw points out.2017-01-03
  • 0
    @AustinMohr Yeah, I thought about it, but then graph H could have vertices on adjacency list in different order than graph G and it could not be enough to just check if (let G[v] be adjacency list of v in G and H[v] be adjacency list of v in H) G[v][0] == H[v][0], G[v][1] == H[v][1], etc. It would require checking if G[v][0] is somewhere in H[v], then if G[v][1] and so on and also check if adjacency lists length is equal and this would take much longer than O(n+m).2017-01-03
  • 0
    @Octothorp you are right, checking adjacency lists in that way would be longer. My answer below assumes that you are given a bijection $f$ and then uses BFS to check if $f$ is a graph isomorphism, which seems to be what you are looking for in your question. If instead you wanted to test whether the *identity function* $f$ is a graph isomorphism -- that is if $E(G)=E(H)$ -- then you can do that in $O(m)$. Note that these are all different answers that depend on the specifics of your problem.2017-01-04

2 Answers 2

0

With some help I came up with the following algorithm:

foreach v in V
    foreach u in G[v]
        i++
        w[u]++
    foreach u in H[v]
        if w[u] == 0: return false // <- on neighbour list of v in H we visited a vertex that we haven't visited in G
        w[u]--
        i--
    if i != 0: return false // <- we visited different number of neighbours in G[v] and H[v]
return true

w is an auxiliary array where I store which vertices I have visited in G. G[v] is a list of neighbours of v in G. The rest is pretty self-explanatory.

If there's something wrong, please comment.

1

Your idea to use DFS or BFS is correct; however, checking for matching degree sequences will not be enough, in general. The reason for this is that non-isomorphic graphs can have matching degree sequences.

To have a better idea of the solution, I think it's best to clearly define what it means for graphs $G$ and $H$ to be "identical". The concept of graph isomorphism is probably what you want here. Two graphs $G$ and $H$ are said to be isomorphic if there exists a bijection $f:V(G) \rightarrow V(H)$ such that for all vertices $u, v$ that are adjacent in G, we have that $f(u)$ and $f(v)$ are adjacent in $H$. Such a function $f$ is said to be a graph isomorphism. Since you stated that $V(G) = V(H)$, I assume that you already have some bijection $f$ and you are wanting to test if $f$ is a graph isomorphism.

To check that $f$ is a graph isomorphism, for each vertex $v$, we can examine each of its neighbors $u$ in $G$ and check that $f(v)$ and $f(u)$ are adjacent in $H$. This can be done in $O(n+m)$ time using BFS.

Note that your problem, deciding whether a bijection $f:V(G) \rightarrow V(H)$ is a graph isomorphism, is much easier than trying to determine whether $G$ and $H$ are ismorphic; that is, whether a graph isomorphism $f$ exists. The complexity of graph isomorphism testing is actually an active area of research. If you are looking for software to do this, I recommend nauty, written by Brendan McKay.