# A sub graph match comparison

**URL:** <https://community.wolfram.com/t/a-sub-graph-match-comparison/16716>\
**Category:** Questions\
**Tags:** graphs-and-networks, wolfram-repositories\
**Created:** [September 12, 2020, 1:11pm UTC](https://community.wolfram.com/t/a-sub-graph-match-comparison/16716 "2020-09-12T13:11:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mmstud](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/mmstud/32/6909_2.png) [@mmstud](https://community.wolfram.com/u/mmstud)\
**Post date:** [September 12, 2020, 1:11pm UTC](https://community.wolfram.com/t/a-sub-graph-match-comparison/16716/1 "2020-09-12T13:11:35Z")

</div>

Hi,

I’m trying to do a comparison between a main graph and a sub graph. Simply put, is there a native function to do this job or should I do search and compare in the the following way:

Triangular grid as a main grap:

```
a = ResourceFunction["WolframModel"][{{x, y}} -> {{x, y},{y, z},{z, x}}, {{1, 2}}, 2, "FinalState"]

```

Triangle:

```
b = ResourceFunction["WolframModel"][{{x, y}} -> {{x, y},{y, z},{z, x}}, {{1, 2}}, 1, "FinalState"]

```

Square:

```
c = ResourceFunction["WolframModel"][{{x, y}} -> {{x, y},{y, z},{z, u},{u, x}}, {{1, 2}}, 1, "FinalState"]

```

Looging for triangle:

```
s = Subgraph[b,b]
Subgraph[a,b] == s -> TRUE

```

Looginf for square:

```
Subgraph[a,c] == s -> FALSE

```

Before I’m going to write a function for the comparison, I’d like to know what is the best way doing this?

---

<div class="post-metadata">

**Author:** ![rohitnamjoshi](https://avatars.discourse-cdn.com/v4/letter/r/f1d935/32.png) [@rohitnamjoshi](https://community.wolfram.com/u/rohitnamjoshi)\
**Post date:** [September 12, 2020, 10:32pm UTC](https://community.wolfram.com/t/a-sub-graph-match-comparison/16716/2 "2020-09-12T22:32:14Z")

</div>

Hi Marko,

Take a look at [IsomorphicGraphQ](http://reference.wolfram.com/language/ref/IsomorphicGraphQ.html).

---

<div class="post-metadata">

**Author:** ![mmstud](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/mmstud/32/6909_2.png) [@mmstud](https://community.wolfram.com/u/mmstud)\
**Post date:** [September 14, 2020, 5:24am UTC](https://community.wolfram.com/t/a-sub-graph-match-comparison/16716/3 "2020-09-14T05:24:30Z")

</div>

Hi Rohit,

I took a look at that, but I’m not sure how to use it:

```
IsomorphicGraphQ[UndirectedGraph[a],UndirectedGraph[b]]

```

That won’t give the desired result. It looks like the function requires the same amount of points in the graph, but in my case, I want to know if subgraph exists in the main graph. The main graph may be a huge grid of points and the subgraph just a small part. I need to know if a small graph exists inside the big graph as directed (and undirected) list of points.

-Marko

---

<div class="post-metadata">

**Author:** ![jasonb](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/jasonb/32/20741_2.png) [@jasonb](https://community.wolfram.com/u/jasonb)\
**Post date:** [September 14, 2020, 6:29pm UTC](https://community.wolfram.com/t/a-sub-graph-match-comparison/16716/4 "2020-09-14T18:29:41Z")

</div>

You need a function to say whether one graph is a subgraph of another, a `SubGraphQ` function.

I recommend the `IGraphM` package, available at [GitHub - szhorvat/IGraphM: IGraph/M is the igraph interface for Mathematica · GitHub](https://github.com/szhorvat/IGraphM). Look for the function `IGSubisomorphicQ`.
