1 Introduction

Finding dense subgraphs is a core problem in graph mining with many applications in diverse domains such as social network analysis (Semertzidis et al., 2019), temporal pattern mining in financial markets (Du et al., 2009), and biological system analysis (Fratkin et al., 2006). Often, many real-world networks vary over time, in which case a sequence of graph snapshots naturally exists. Consequently, mining dense subgraphs over time has gained attention in data mining literature (Jethava & Beerenwinkel, 2015; Semertzidis et al., 2019; Rozenshtein et al., 2020; Galimberti et al., 2020).

Our goal is to find dense subgraphs in a temporal network. To measure the density, we will use the definition of the ratio between the number of induced edges and vertices. We should point out that there are other definitions of density such as the proportion of edges. However, our choice is popular since the densest subgraph can be found in polynomial time (Goldberg, 1984) and approximated efficiently (Charikar, 2000).

Given a graph sequence, there are natural extremes to find the densest subgraphs: the first approach is to find a common subgraph that maximizes the sum of densities for individual snapshots, as proposed by Semertzidis et al. (2019) among other techniques. The other approach is to find the densest subgraphs for each snapshot individually.

In this paper, we study the problem that bridges the gap between these two extremes, namely, we seek dense subgraphs in a temporal network that are allowed to vary over time to a certain degree. More formally, given a graph sequence \({{\mathcal {G}}}\) and parameter \(\alpha\), we seek a sequence of subgraphs, with pairwise Jaccard index at least \(\alpha\), such that the sum of densities is maximized.

We demonstrate the differences in the following toy example.

Fig. 1
figure 1

Toy graphs used in Example 1 and 2

Example 1

Consider a graph sequence \((G_1,G_2,G_3)\) shown in Fig. 1, each graph consisting of 6 vertices and varying edges. We denote the density induced by the vertex set \(S_i\) by \(d \mathopen {}\left( S_i\right)\), defined as the ratio between number of induced edges and vertices, \(d \mathopen {}\left( S_i\right) = \frac{{\left| E(S_i)\right| }}{{\left| S_i\right| }}\). We define the sum of densities as \(\sum _{i=1}^{3} d \mathopen {}\left( S_i\right)\).

The densest common subgraph, that is, a single common subgraph over each snapshot maximizing the sum of densities, is \(S = (a,b,d,e,f)\). The density sum induced by S is \(\frac{4}{5} + \frac{5}{5} + \frac{8}{5} = 3.40\).

On the other hand, the densest subgraphs for individual snapshots are \(S_1' = (a,b,d)\), \(S_2' = (a,b,c,f)\), and \(S_3' = (a,b,d,e,f)\). The sum of densities of individually densest subgraphs is given by \(\frac{3}{3} + \frac{6}{4} + \frac{8}{5} = 4.1\).

Note that the Jaccard index between set S and T is defined as \(J \mathopen {}\left( S, T\right) = \frac{|S \cap T|}{|S \cup T|}\). Therefore, \(J \mathopen {}\left( S_1', S_2'\right) = \frac{3}{5} = 0.6\), \(J \mathopen {}\left( S_1', S_3'\right) = \frac{4}{5} = 0.8\), and \(J \mathopen {}\left( S_2', S_3'\right) = \frac{3}{6} = 0.5\). Therefore, \(\min _{i < j} J(S_i', S_j')\) is 0.5.

Let \(\alpha = 0.6\) and let \(S_1 = (a,b,d,f)\), \(S_2 = (a,b,c,f)\), and \(S_3 = (a,b,d,f)\). Then, \(J \mathopen {}\left( S_1, S_2\right) = \frac{3}{5} = 0.6\), \(J \mathopen {}\left( S_1, S_3\right) = \frac{4}{4} = 1\), and \(J \mathopen {}\left( S_2, S_3\right) = \frac{3}{5} = 0.6\). The total density induced by the sets with pairwise Jaccard index at least 0.6 is given by \(\frac{4}{4} + \frac{6}{4} + \frac{5}{4} = 3.75\). Hence, three different density results are ordered as \(3.4 \le 3.75 \le 4.1\).

We also consider an alternative problem, where we incorporate the Jaccard index into an objective function instead of having it as a constraint. Here, we reward similar graphs over the snapshots. More formally, given a graph sequence \({{\mathcal {G}}}\) and a parameter \(\lambda\), we seek a sequence of subgraphs, such that the sum of densities plus the sum of Jaccard indices, weighted by \(\lambda\), is maximized.

We demonstrate the objective in the following example.

Example 2

Consider a graph sequence \((G_1,G_2,G_3)\) shown in Fig. 1. Given a weight parameter \(\lambda\) and a sequence of subgraphs \((S_1, S_2, S_3)\), we define our objective function as \(\sum _{i=1}^{3} d \mathopen {}\left( S_i\right) + \lambda \sum _{i < j} J \mathopen {}\left( S_i, S_j\right)\).

Assume that we set \(\lambda = 0.3\) and select \(S_1 = (a,b,d,f)\), \(S_2 = (a,b,c,f)\), and \(S_3 = (a,b,d,f)\). The sum of densities is \(\frac{4}{4} + \frac{6}{4} + \frac{5}{4} = 3.75\). Here, \(J \mathopen {}\left( S_1, S_2\right) = \frac{3}{5} = 0.6\), \(J \mathopen {}\left( S_1, S_3\right) = \frac{4}{4} = 1\), and \(J \mathopen {}\left( S_2, S_3\right) = \frac{3}{5} = 0.6\). Therefore, our objective is equal to \(3.75 + 0.3 \times (0.6 + 1 + 0.6) = 4.41\).

We show that both of our problems are NP-hard and consequently propose greedy algorithms. For the constrained version of the problem, we propose an iterative algorithm that starts from the common densest subgraph solution and greedily improves the solution by adding or removing vertices.

For the second problem, we propose two algorithms. The first approach is an iterative algorithm where we start either with the common densest subgraph or a set of the densest subgraphs for each individual snapshot and then iteratively improve each individual snapshot. The improvement step is done with a classic technique used to approximate the densest subgraph (in a single graph) (Asahiro et al., 2000; Charikar, 2000). We start with the complete snapshot, and iteratively seek out the vertex so that the remaining graph yields the largest score. We remove the vertex, and the procedure is repeated until no vertices remain; the best subgraph for that snapshot is selected. This is repeated for each snapshot until no improvement is possible. The second algorithm uses a similar approach with the following differences. The algorithm does not iterate over snapshots. Instead, we search for the best snapshot and the best vertex in that snapshot and delete the vertex. We continue until there are no vertices left, and select the best set of subgraphs seen during the deletion.

The appeal of this approach is that, when dealing with a single graph, finding the next vertex can be done efficiently using a priority queue (Charikar, 2000; Asahiro et al., 2000). We cannot use this approach directly due to the updates in Jaccard indices. Instead, we maintain a set of priority queues that allow us to find vertices quickly in practice.

This paper extends the earlier paper (Wickrama Arachchi & Tatti, 2023) by considering maximizing the density while constraining Jaccard indices.

The remainder of the paper is organized as follows. In Sect. 2, we provide preliminary notation along with the formal definitions of our optimization problems. All our algorithms and their running times are presented in Sect. 3. Related work is discussed in Sect. 4. In Sect. 5 we present an extensive experimental study both with synthetic and real-world datasets followed by two case studies. Finally, Sect. 6 summarizes the paper and provides directions for future work. This paper is an extension of a conference paper (Wickrama Arachchi & Tatti, 2023).

2 Preliminary notation and problem definition

We begin by providing preliminary notation and formally defining our problem.

Our input is a sequence of graphs \({{\mathcal {G}}}= (G_1, \ldots , G_k)\), where each snapshot \(G_i = (V, E_i)\) is defined over the same set of vertices. We denote the number of vertices and edges by \(n = {\left| V\right| }\) and \(m_i = {\left| E_i\right| }\), or \(m = {\left| E\right| }\), if i is omitted.

Given a graph \(G = (V, E)\), and a set of vertices \(S \subseteq V\), we define \(E(S) \subseteq E\) to be the subset of edges having both endpoints in S.

As mentioned before, our goal is to find dense subgraphs in a temporal network, and for that, we need to quantify the density of a subgraph. More formally, assume an unweighted graph \(G = (V, E)\), and let \(S \subseteq V\). We define the density \(d \mathopen {}\left( S\right)\) of a single vertex set S and extend this definition for a sequence of subgraphs \({{\mathcal {S}}}= (S_1, \ldots , S_k)\) by writing

$$\begin{aligned} d \mathopen {}\left( S_i\right) = \frac{{\left| E(S_i)\right| }}{{\left| S_i\right| }} \quad \quad \text {and}\quad \quad d \mathopen {}\left( {{\mathcal {S}}}\right) = \sum _{i = 1}^k d \mathopen {}\left( S_i\right) \quad . \end{aligned}$$

We will use the Jaccard index to measure the similarity between two subgraphs. More formally, given two sets of vertices S and T, we write

$$\begin{aligned} J \mathopen {}\left( S, T\right) = \frac{|S \cap T|}{|S \cup T|}\quad . \end{aligned}$$

Given a sequence of graph snapshots and an input parameter \(\alpha\), our goal is to find a collection of subsets of vertices, one per each snapshot, such that the sum of the densities of subgraphs is maximized while maintaining pairwise Jaccard coefficient between the sets to be at least \(\alpha\).

Problem 2.1

[Jaccard Constrained Densest Subgraphs (JCDS)] Given a graph sequence \({{\mathcal {G}}}= (G_1,\dots ,G_k)\), with \(G_i = (V, E_i)\), and a real number \(\alpha \in [0,1]\), find a collection of subset of vertices \({{\mathcal {S}}}= (S_1,\dots ,S_{k})\), such that \(d \mathopen {}\left( {{\mathcal {S}}}\right)\) is maximized and \(J \mathopen {}\left( S_i, S_j\right) \ge \alpha\) for all i and j.

We will consider two extreme cases. The first case is \(\alpha = 1\), which we refer to as densest common subgraph or DCS that maximizes total density. This problem can be solved by first flattening the graph sequence into one weighted graph, where an edge weight is the number of snapshots in which an edge occurs. The problem is then a standard (weighted) densest subgraph problem that can be solved using the method given by Goldberg (1984) in \({\mathcal {O}} \mathopen {}\left( n (n + m )(\log n + \log k)\right)\) time. The other extreme case is \(\alpha = 0\) which can be solved by solving the densest subgraph problem for each individual snapshot.

The main difference from prior studies (Jethava & Beerenwinkel, 2015; Semertzidis et al., 2019) is that we allow the subsets to be varied within a given margin (which is defined by Jaccard coefficient), without enforcing subsets to be fully identical. In other words, we extend the idea of the common densest subgraph in which \(\alpha = 1\), for a more generalized case where \(\alpha\) can be less than one.

In addition to the hard constraint version, we consider a variant where we incorporate the constraints directly into the optimization score. Ideally, we would like each subgraph to have high density, and share as many vertices as possible with each other. This leads to the following score and optimization problem. More specifically, given a weight parameter \(\lambda\) and a sequence of subgraphs \({{\mathcal {S}}}= (S_1, \ldots , S_k)\), we define a score

$$\begin{aligned} q \mathopen {}\left( {{\mathcal {S}}}; \lambda \right) = d \mathopen {}\left( {{\mathcal {S}}}\right) + \lambda \sum _{i=1}^{k} \sum _{j = i + 1}^{k} J \mathopen {}\left( S_i, S_j\right) \quad . \end{aligned}$$

Problem 2.2

[Jaccard Weighted Densest Subgraphs (JWDS)] Given a graph sequence \({{\mathcal {G}}}= (G_1,\dots ,G_k)\), with \(G_i = (V, E_i)\), and a real number \(\lambda\), find a collection of subset of vertices \({{\mathcal {S}}}= (S_1,\dots ,S_{k})\), such that \(q \mathopen {}\left( {{\mathcal {S}}}\right)\) is maximized.

The purpose of user parameter \(\lambda\) is to maintain a predefined balance between the two terms of \(q \mathopen {}\left( {{\mathcal {S}}}\right)\); namely the density term and Jaccard similarity term. Here we will consider changing \(\lambda\) according to our needs. If we want the collection of subgraphs to be more identical, we set \(\lambda\) to be very large. In this case, we favor the vertex sets who incur higher overlap with other snapshots. Nevertheless, if we are more focused on the density than the similarity between sets, we assign a small value to \(\lambda\) which is less than 1.

3 Algorithms

The problem of finding a common subgraph which maximizes the sum of densities can be solved optimally in polynomial time. Moreover, if we set \(\lambda = 0\), then we can solve the problem by finding optimal dense subgraphs for each snapshot individually. However, JCDS and JWDS are both \({\textbf {NP}}\)-hard. The proof of the propositions is in “Appendix A.1–A.2”.

Proposition 1

JCDS is NP-hard.

Proposition 2

JWDS is NP-hard.

Next, we will present our heuristic algorithms.

3.1 Finding Jaccard-constrained densest subgraphs

First, we consider a straightforward greedy algorithm for JCDS. The idea of the proposed iterative algorithm is as follows. We start by solving the densest common subgraph problem, that is, JCDS with \(\alpha = 1\).

We initialize the sets to be the densest common subgraph. Note that the Jaccard constraint is automatically satisfied. We then try to improve the sum of the densities by either adding or removing vertices to each subset, while satisfying the pairwise Jaccard similarity coefficient constraints. We repeat the same process until the algorithm converges. The objective value only either improves or stays constant at the end of each iteration. Thus our algorithm always converges. The pseudo-code for the algorithm is given in Algorithm 1.

Algorithm 1
figure a

\(\textsc {Hard} ({{\mathcal {G}}},\alpha )\), finds \(\mathcal {S}\) with good \(d \mathopen {}\left( \mathcal {S}\right)\) s.t. \(\min J \mathopen {}\left( S_i, S_j\right) \ge \alpha\).

To quickly test each vertex, we maintain the sizes of intersections and the unions between \(S_i\) and \(S_j\). This leads to the following running time for a single iteration.

Proposition 3

Assume a graph sequence \(G_1, \ldots , G_k\) with n vertices and total \(m = \sum _i m_i\) edges. Then the running time of a single iteration of Hard is in \({\mathcal {O}} \mathopen {}\left( nk^2 + m\right)\).

Proof

Computing the density gain \(d \mathopen {}\left( {{\mathcal {S}}}'\right) - d \mathopen {}\left( {{\mathcal {S}}}\right)\) requires iterating over the edges adjacent to v. Consequently, testing over all v and i results in visiting every edge twice, amounting to a total of \({\mathcal {O}} \mathopen {}\left( m\right)\) time.

Testing the Jaccard constraint can be done in \({\mathcal {O}} \mathopen {}\left( k\right)\) time since we maintain the sizes of intersections and the unions between \(S_i\) and \(S_j\). Once \(S_i\) is updated, updating these sizes can be updated in \({\mathcal {O}} \mathopen {}\left( k\right)\) time. Since there are n vertices in k snapshots, the claim follows. \(\square\)

3.2 Finding Jaccard-weighted densest subgraphs

In this section, we present two algorithms that we will use to find a sequence of subgraphs with good score \(q \mathopen {}\left( \cdot ; \lambda \right)\).

The first algorithm is as follows. We start with an initial candidate \({{\mathcal {S}}}\). This set is either the solution of the densest common subgraph or the densest subgraphs of each individual snapshot; we test both and select the best end result.

To improve the initial set we employ the strategy used in Asahiro et al. (2000), Charikar (2000) when approximating the densest subgraph: Here, the algorithm starts with the whole graph and removes a vertex with the minimum degree, or equivalently, removes a vertex such that the remaining subgraph has the highest density. This is continued until no vertices remained, and among the tested subgraphs the one with the highest density is selected.

We employ a similar strategy. For a snapshot \(G_i\), we start with \(S_i = V\), and then iteratively remove the vertices so that the score is maximal. After removing all vertices, we pick the subgraph for \(S_i\) which maximizes our objective \(q \mathopen {}\left( {{\mathcal {S}}}; \lambda \right)\). We iterate over all snapshots, we keep on modifying the sets until the algorithm converges. Since the objective value either only increases or stays constant at the end of each iteration, the algorithm always converges. The pseudo-code for this approach is given in Algorithm 2.

Algorithm 2
figure b

\(\textsc {Itr} ({{\mathcal {G}}},\lambda , {{\mathcal {S}}})\), finds subgraphs with good \(q \mathopen {}\left( \cdot ; \lambda \right)\)

Next, we will show that, due to the choice of the initial sets, Itr is a 2-approximation algorithm.

Proposition 4

Assume a graph sequence \({{\mathcal {G}}}\) and let \({{\mathcal {S}}}^*\) be the optimal solution. Let \({{\mathcal {S}}}\) be the subgraph sequence consisting of the densest common subgraph. Let \({{\mathcal {S}}}'\) be the subgraph sequence consisting of the densest subgraphs for each snapshot. Then

$$\begin{aligned} q \mathopen {}\left( {{\mathcal {S}}}^*\right) \le 2 \max ( q \mathopen {}\left( {{\mathcal {S}}}\right) , q \mathopen {}\left( {{\mathcal {S}}}'\right) )\quad . \end{aligned}$$

Before proving this result we should point out that any sequence with a common subgraph in place of \({{\mathcal {S}}}\) will yield the result. However, choosing the densest common subgraph is a sensible choice.

Proof

Let us write \(f({{\mathcal {S}}}) = \lambda \sum _{i=1}^{k} \sum _{j = i + 1}^{k} J \mathopen {}\left( S_i, S_j\right)\). Note that \({{\mathcal {S}}}'\) maximizes the density term in \(q \mathopen {}\left( \cdot \right)\) while \({{\mathcal {S}}}\) maximises the Jaccard term in \(q \mathopen {}\left( \cdot \right)\). Thus,

$$\begin{aligned} \begin{aligned} q \mathopen {}\left( {{\mathcal {S}}}^*\right)&= d \mathopen {}\left( {{\mathcal {S}}}^*\right) + f({{\mathcal {S}}}^*) \\&\le 2 \max ( d \mathopen {}\left( {{\mathcal {S}}}^*\right) , f({{\mathcal {S}}}^*)) \le 2 \max ( d \mathopen {}\left( {{\mathcal {S}}}'\right) , f({{\mathcal {S}}})) \le 2 \max ( q \mathopen {}\left( {{\mathcal {S}}}\right) , q \mathopen {}\left( {{\mathcal {S}}}'\right) ), \end{aligned} \end{aligned}$$

proving the claim. \(\square\)

Since iterative phase cannot decrease the score, the following approximation result is imminent.

Corollary 3.1

Let \({{\mathcal {S}}}^*\) be the solution to JWDS and let \({{\mathcal {S}}}= \textsc {Itr} ({{\mathcal {G}}}, \lambda )\) be the sequence produced by Itr. Then \(q \mathopen {}\left( {{\mathcal {S}}}^*, \lambda \right) \le 2 q \mathopen {}\left( {{\mathcal {S}}}, \lambda \right)\).

Our second algorithm is similar to Itr. In Algorithm 2 we consider each snapshot separately and peel off vertices. In our second algorithm, we initialize each \(S_i\) with V. In each iteration, we find a snapshot \(S_i\) and a vertex v so that the remaining subgraph sequence is maximized. We remove the vertex and continue until no vertices are left. In the process, we choose the one which maximizes our objective function. The pseudo-code for this method is given in Algorithm 3.

Algorithm 3
figure c

\(\textsc {Grd} ({{\mathcal {G}}},\lambda )\), finds subgraphs with good \(q \mathopen {}\left( \cdot ; \lambda \right)\)

The bottleneck in both algorithms is finding the next vertex to delete. Let us now consider how we can speed up this selection. To this end, select \(S_i\) and let \(v \in S_i\). Let us write \({{\mathcal {S}}}'\) to be \({{\mathcal {S}}}\) with \(S_i\) replaced with \(S_i \setminus \left\{ v\right\}\). We can write the score difference between \({{\mathcal {S}}}\) and \({{\mathcal {S}}}'\) as

$$\begin{aligned} q \mathopen {}\left( {{\mathcal {S}}}'\right) - q \mathopen {}\left( {{\mathcal {S}}}\right) = \frac{{\left| E(S_i)\right| } - \deg v}{{\left| S_i\right| } - 1} - \frac{{\left| E(S_i)\right| }}{{\left| S_i\right| }} + \sum _{j \ne i} J \mathopen {}\left( S_i \setminus \left\{ v\right\} , S_j\right) - J \mathopen {}\left( S_i, S_j\right) \quad . \end{aligned}$$
(1)

Let us first consider Grd. To find the optimal v and i, we will group the vertices in \(S_i\) such that the the sum in Eq. 1 is equal for the vertices in the same group. In order to do that we group the vertices based on the following condition: if two vertices \(u,v \in S_i\) belong to the exactly same \(S_j\) for each j, that is, \(u \in S_j\) if and only if \(v \in S_j\), then u and v belong to the same group. More formally, let us first define \(act \mathopen {}\left( v\right) = \left\{ j \mid v \in S_j\right\}\) to be the set of indices of all \(S_j\) that have v as a member. The function \(act \mathopen {}\left( \cdot \right)\) induces a partition \(\mathcal {P}_i\) of \(S_i\): each group in \(\mathcal {P}_i\) consists of vertices v having the same \(act \mathopen {}\left( v\right)\).

Select \(P \in \mathcal {P}_i\). Since the sum in Eq. 1 is constant for all vertices in P, the vertex in P maximizing Eq. 1 must have the smallest degree. Thus, we maintain the vertices in P in a priority queue keyed by the degree. We also maintain the difference of the Jaccard indices, the sum in Eq. 1. In order to maintain the difference, we maintain the sizes of intersection \({\left| S_i \cap S_j\right| }\) and the union \({\left| S_i \cup S_j\right| }\) for all i and j. To find the optimal v and i, we find the vertex with the smallest degree in each group, and then compare these candidates among different groups.

This data structure leads to the following running time.

Proposition 5

Assume a graph sequence \(G_1, \ldots , G_k\) with n vertices and total \(m = \sum _i m_i\) edges. Let \(\mathcal {P}_{ir}\) be the groups of \(S_i\) (based on the vertex memberships in other snapshots) when deleting the rth vertex. Define \(\Delta = \max {\left| \mathcal {P}_{ir}\right| }\). Then the running time of Grd is in

$$\begin{aligned} {\mathcal {O}} \mathopen {}\left( nk^2 \Delta + m \log n + k^2 n(k + \log n)\right) \subseteq {\mathcal {O}} \mathopen {}\left( n^2k^2 + m \log n + k^3 n\right) \quad . \end{aligned}$$

See “Appendix A.3” for proof.

We should point out that the running time depends on \(\Delta\), the number of queues in a single snapshot. This number may be as high as the number of vertices, n, but ideally \(\Delta \ll n\).

The same data structure can be also used Itr. The only difference is that we do not select optimal i; instead, i is fixed when looking for the next vertex to delete. Trivial adjustments to the proof of Prop. 5 imply the following claim.

Proposition 6

Assume a graph sequence \(G_1, \ldots , G_k\) with n vertices and total \(m = \sum _i m_i\) edges. Let \(\mathcal {P}_{ir}\) be the groups of \(S_i\) (based on the vertex memberships in other snapshots) when deleting rth vertex. Define \(\Delta = \max {\left| \mathcal {P}_{ir}\right| }\). Then the running time of a single iteration of Itr is in

$$\begin{aligned} {\mathcal {O}} \mathopen {}\left( nk^2 \Delta + m \log n + k^2 n(k + \log n)\right) \subseteq {\mathcal {O}} \mathopen {}\left( n^2k^2 + m \log n + k^3 n\right) \quad . \end{aligned}$$

4 Related work

In this section we discuss previous studies on discovering the densest subgraph in a single graph, the densest common subgraph over multiple graphs, overlapping densest subgraphs, and other types of density measures.

The densest subgraph: Given an undirected graph, finding the subgraph which maximizes density has been first studied by Goldberg (1984) where an exact, polynomial time algorithm which solves a sequence of min-cut instances is presented. Asahiro et al. (2000) provided a linear time, greedy algorithm proved to be a 1/2-approximation algorithm by Charikar (2000). The idea of the algorithm is that at each iteration, a vertex with the minimum degree is removed, and then the densest subgraph among all the produced subgraphs is chosen.

Several variants of the densest subgraph problem constrained on the size of the subgraph \({\left| S\right| }\) have been studied: finding the densest k-subgraph (\({\left| S\right| } = k\)) (Feige et al., 2001; Khot, 2006; Asahiro et al., 2000), at most k-subgraph (\({\left| S\right| } \le k\)) (Andersen & Chellapilla, 2009; Khuller & Saha, 2009), and at least k-subgraph (\({\left| S\right| } \ge k\)) (Andersen & Chellapilla, 2009; Khuller & Saha, 2009). Unlike the densest subgraph problem, when the size constraint is applied, the densest k-subgraph problem becomes NP-hard (Feige et al., 2001). Furthermore, there is no polynomial time approximation scheme (PTAS) (Khot, 2006). Approximating the problem of finding at most k-subgraph is shown as hard as the densest k-subgraph problem by  Khuller and Saha (2009). To find exactly k-size densest subgraph, Bhaskara et al. (2010) gave an \({\mathcal {O}} \mathopen {}\left( n^{1/4 + \epsilon }\right)\)-approximation algorithm for every \(\epsilon > 0\) that runs in \(n^{ {\mathcal {O}} \mathopen {}\left( 1/\epsilon \right) }\) time. Andersen and Chellapilla (2009) provided a linear time 1/3-approximation algorithm for at least k densest subgraph problem.

The densest common subgraph over multiple graphs: Jethava and Beerenwinkel (2015) extended the densest subgraph problem (DCS) for the case of multiple graph snapshots. As a measure, the authors’ goal was to maximize the minimum density. Moreover, Semertzidis et al. (2019) introduced several variants of this problem by varying the aggregate function of the optimization problem, one variant, BFF-AA, is closely related to the DCS problem discussed in Sect. 2. DCS which maximizes the total density can be solved exactly through a reduction to the densest subgraph problem, and is consequently polynomial. The hardness of DCS variants has been addressed (Charikar et al., 2018). For a survey on the densest subgraph problem and its variants, we refer the reader to a recent survey by Lanciano et al. (2023).

Overlapping densest subgraphs of a single graph: Finding multiple dense subgraphs in a single graph which allows graphs to be overlapped is studied by adding a hard constraint to control the overlap of subgraphs (Balalau et al., 2015). Later, Galbrun et al. (2016) formulated the same problem adding a penalty in the objective function for the overlap. The difference between our problem and the works of Balalau et al. (2015) and Galbrun et al. (2016) is that our goal is to find a collection of dense subgraphs over multiple graph snapshots (one dense subgraph for each graph snapshot) while they discover a set of dense subgraphs within a single graph. Due to this difference, we want to reward similar subgraphs while the authors want to penalize similar subgraphs.

Other density measures: We use the ratio of edges over the vertices as our measure as it allows us to compute it efficiently. Alternative measures have been also considered. One option is to use the proportion of edges instead, that is, \({\left| E\right| } / {{\left| V\right| } \atopwithdelims ()2}\). The issue with this measure is that a single edge yields the highest density of 1. Moreover, finding the largest graph with the edge proportion of 1 is equal to finding a clique, a classic problem that does not allow any good approximation (Håstad, 1996). As an alternative approach, Tsourakakis et al. (2013) proposed finding subgraphs with large score \({\left| E\right| } - \alpha {{\left| V\right| } \atopwithdelims ()2}\). Optimizing this measure is an NP-hard problem but an algorithm similar to the one given by Asahiro et al. (2000) leads to an additive approximation guarantee. In a similar vein, Tatti (2019) considered subgraphs maximizing \({\left| E\right| } - \alpha {\left| V\right| }\) and showed that they form a nested structure similar to k-core decomposition. An alternative measure called triangle-density has been proposed by Tsourakakis (2015) as a ratio of triangles and vertices, possibly producing smaller graphs. Like the density, optimizing this measure can be done in polynomial time. We leave adopting these measures as a future work.

5 Experimental evaluation

The goal of this section is to experimentally evaluate our algorithms. We first generate several synthetic datasets and plant dense subgraph components, in each snapshot and test how well our algorithms discover the ground truth. Next, we study the performance of the algorithm on real-world temporal datasets in terms of running time. We compare our results with the solutions obtained with the densest common subgraph which maximizes the total density (Semertzidis et al., 2019) and the sum of densities of individually densest subgraphs (Goldberg, 1984). Finally, we present interpretative results from two case studies.

We implemented the algorithms in Python.Footnote 1 and performed the experiments using a 2.4GHz Intel Core i5 processor and 16GB RAM.

Table 1 Characteristics of synthetic datasets
Table 2 Computational statistics from the experiments with the synthetic datasets using Hard algorithm
Table 3 Computational statistics from the experiments for synthetic datasets using Itr and Grd algorithms

Synthetic datasets: The primary goal of the experiments with the synthetic data is to compare the algorithms, and demonstrate the effect of the parameter \(\alpha\) (and \(\lambda\)). For this purpose, we created several synthetic datasets, which we will describe next.

Each dataset consists of k graphs given as \((G_1, \ldots , G_k)\). We split the vertex set into dense and sparse components \(V^d\) and \(V^s\). To generate the ith snapshot we create two components \(V^d_i\) and \(V^s_i\) by starting from \(V^d\) and \(V^s\) and moving vertices from \(V^s\) to \(V^d\) with a probability of \(\eta _i\). The probability \(\eta _i\) is selected randomly for each snapshot from a uniform distribution [0.01, 0.09]. Once the vertices are generated, we sample the edges using a stochastic block model, with the edge probabilities being \(p_d\), \(p_s\), and \(p_c\) for dense component, sparse component, and cross edges, respectively.

We tested our algorithms with several synthetic datasets by varying the component sizes and the edge probabilities. We highlight the results for 3 such datasets, as the conclusions for the other datasets were the same. The statistics and the related parameters of the datasets are given in Table 1.

We consider \(V^d\) as the ground truth vertex set and we denote the density of \(V_d\) by \(d_{true}\). We should point out that the datasets have a prominent dense component, so we expect to find the ground truth, or a set that is very close to the ground truth.

Results of synthetic datasets: We report our results for the JCDS and JWDS problems in Tables 2 and 3 respectively.

First, we compare the discovered density, \(d_{ dis }\) in Table 2, with the ground truth density, \(d_{true}\) in Table 1. For all synthetic datasets, Hard obtains approximately similar densities or better densities compared to the ground truth density.

We compare the discovered sets against the ground truth by computing the Jaccard index between the sets, shown in the \(\rho\) column in Table 2. We see that we achieve high overlap with the ground truth: \(\rho\) column shows at least 0.97 for all the datasets. High values of \(\rho\) are expected as our synthetic datasets have a prominent dense component.

Next let us examine \(d_{ dis }\) column of Table 2 separately for each dataset. As expected, \(d_{ dis }\) increases as we decrease \(\alpha\). Finally, we see that the minimum Jaccard index \(J_{ min }\) between discovered vertices increases as \(\alpha\) increases.

Next, we consider the results of the JWDS problem, shown in Table 3. First, we observe that the discovered density values \(d_{ dis }\) approximately match each other, that is, both Itr and Grd perform equally well in terms of densities. A similar result holds also for the scores \(q \mathopen {}\left( \cdot ; \lambda \right)\) and minimum Jaccard coefficients \(J_{ min }\). However, Itr runs faster than Grd. This is probably due to the fact that Grd takes more time to select the next vertex to delete, which is the bottleneck in both algorithms despite having the same asymptotic time complexity per iteration in Itr and overall time complexity in Grd. Next, we compare the discovered sets to the ground truth, given in columns \(\rho\). We see both algorithms give similar values which indicates equally good performance of Itr and Grd.

Our next step is to study the effect of the input parameters: \(\alpha\) or \(\lambda\). First, we observe Fig. 2 which demonstrates \(\rho\) and discovered density \(d_{ dis }\) as a function of \(\alpha\). In Fig. 2a, we see that \(\rho\) remains relatively constant in 0.97 at the start and as we increase \(\alpha\) beyond 0.45 it starts to decrease more prominently. That is because \(J_{ min }\) which gives the minimum pairwise Jaccard coefficient between the planted dense components is 0.45 for Syn-1 dataset. When we set \(\alpha > 0.45\), Hard deviates from the ground truth as the ground truth no longer satisfies the constraints. Let us now consider Fig. 2b which demonstrates how the discovered sum of densities changes with respect to \(\alpha\). In Fig. 2b, we observe that \(d_{ dis }\) remains constant until 0.45 and decreases further as we increase \(\alpha\).

Next, we observe Fig. 3 which demonstrates \(\rho\) as a function of \(\lambda\). In Fig. 3a, we see that \(\rho\) gradually decreases as we increase \(\lambda\). This is due to the fact that when we increase the weight of the constraint part of \(q\), the algorithms try to find dense sets with higher Jaccard coefficients which eventually forces to deviate from their ground truth. Furthermore, if we set \(\lambda = 2\), we can see a drastic change in \(\rho\).

Let us now consider Fig. 3b which demonstrates how the discovered sum of densities changes with respect to \(\lambda\). We see the decreasing trend showing that the Jaccard term in the objective function starts to dominate with the increase of \(\lambda\). As expected, in Fig. 3c we see that both Itr and Grd yield increasing scores when we increase \(\lambda\).

Fig. 2
figure 2

Jaccard index to the ground truth \(\rho\) as a function of \(\alpha\) for Hard in a and discovered density \(d_{ dis }\) as a function of \(\alpha\) for Hard in b. This experiment was performed using Syn-1 dataset

Fig. 3
figure 3

Average Jaccard index to the ground truth \(\rho\) as a function of \(\lambda\) in a. Discovered density \(d_{ dis }\) as a function of \(\lambda\) in b. Scores \(q \mathopen {}\left( \cdot ; \lambda \right)\) as a function of \(\lambda\) in c. This experiment was performed using Syn-1 dataset

Fig. 4
figure 4

The discovered density \(d_{ dis }\) as a function of \(J_{ min }\) for Hard and Itr in a and discovered density \(d_{ dis }\) as a function of \(J_{ avg }\) for Hard and Itr in b. This experiment was performed using Syn-2 dataset

Note that the problems JCDS and JWDS optimize two different objective functions. The JCDS problem focuses solely on maximizing the sum of the induced densities while satisfying the similarity constraints. On the other hand, JWDS maximizes the sum of total density and weighted Jaccard indices without explicitly enforcing similarity constraints over the solution sets. Next, we study how the discovered density behaves as a function of \(J_{ min }\) and \(J_{ avg }\) for Hard and Itr algorithms, as shown in Fig. 4. Let us first observe Fig. 4a. We see that Hard achieves higher density for low \(J_{ min }\) values. On the other hand, Itr performs better with high \(J_{ min }\) values even though Itr optimizes a different problem. The discrepancy is due to Hard being stuck in a local minimum. Nevertheless, the densities are similar for both algorithms.

In Fig. 4b, we observe that Itr achieves higher average Jaccard indices as Hard for the same density. This is expected since Itr uses \(J_{ avg }\) as a part of its objective function whereas Hard uses minimum Jaccard index.

Table 4 Characteristics of real-world datasets
Table 5 Computational statistics from the experiments with real-world datasets using Hard algorithm
Table 6 Computational statistics from the experiments for real-world datasets

Real-world datasets: We consider 6 publicly available, real-world datasets. The details of the datasets are shown in Table 4. Twitter-# (Tsantarliotis & Pitoura, 2015)Footnote 2 is a hashtag network where vertices correspond to hashtags and edges corresponds to the interactions where two hashtags appear in a tweet. This dataset contains 15 such daily graph snapshots in total. EnronFootnote 3 is a popular dataset which contains email communication data within senior management of Enron company. It contains 183 daily snapshots in which the daily email count is at least 5. Facebook (Viswanath et al., 2009)Footnote 4 is a network of Facebook users in New Orleans regional community. It contains a set of Facebook wall posts among these users from the 9th of June to the 20th of August, 2006. StudentsFootnote 5 is an online message network at the University of California, Irvine. It spans over 122 days. Twitter-user (Rozenshtein et al., 2020)Footnote 6 is a network of twitter users in Helsinki 2013. The edges correspond to the mentions of users. Tumblr (Leskovec et al., 2009)Footnote 7 contains phrases or quote mentions that appeared in blogs and news media. It contains author and meme interactions of users over 3 months from February to April 2009.

Results of real-world datasets: We report the results obtained from the experiments with real-world datasets for the JCDS problem in Table 5 and for the JWDS problem in Table 6. In Table 5, we can see that the discovered density values \(d_{ dis }\) exceed the density of the common subgraph \(d_{ dcs }\) (see column \(d_{ dcs }\) in Table 4). As expected, we can observe that \(d_{ dis }\) increases as \(\alpha\) decreases. Moreover, we see that running time which is shown in the time column increases when we increase \(\alpha\). Furthermore, we see in the i column that Hard converges in less than 5 iterations which is reasonable in practice. Finally, we see that \(J_{ min }\) and \(J_{ avg }\) increase as \(\alpha\) increases, as expected.

Next, let us focus on the performance of Itr and Grd algorithms with real-world datasets that are given in Table 6. Note that, as a normalization step, we set \(\lambda = \frac{d_{dcs}}{k} \lambda '\) for the real-world experiments where \(\lambda\) is the parameter in \(q \mathopen {}\left( \cdot ; \lambda \right)\).

First, we compare the scores \(q\) obtained using Itr and Grd. As we can see, apart from the cases in Tumblr dataset, Itr achieves a greater score than Grd. As expected, we see that \(q\) and \(J_{ avg }\) increase and \(d_{ dis }\) decreases as a function of \(\lambda '\) for both algorithms. Furthermore, we observe in time columns, that Itr runs faster than Grd. Next, let us observe column i which gives the number of iterations with Itr algorithm. We can see that we have at most 13 number of iterations which is reasonable to deal with real-world datasets.

Next, we study the effect of \(\lambda '\) on \(J_{ avg }\) across the real-world datasets as shown in Fig. 5. In general, we observe that \(J_{ avg }\) has an increasing trend with \(\lambda '\) up to a certain value and stays almost constant afterwards.

Fig. 5
figure 5

Average Jaccard index \(J_{ avg }\) as a function of \(\lambda '\) for the Itr algorithm

Table 7 Twitter hash tags discovered for Twitter-8 dataset
Table 8 Authors discovered for DBLP dataset and the number in parentheses indicates the size of the set

Case studies: In this section, we present two case-studies and analyze their results which illustrate trending twitter hash tags over a span of 8 days and strongly connected DBLP co-authorships over a decade, under several Jaccard-constrained and weighted environments.

Twitter-8 dataset: Twitter-8 contains a hashtag network from November 2013. This dataset was created by extracting the first 8 daily graph snapshots from the Twitter-# dataset. Here, each vertex of the graph represents a specific hashtag. As seen in the tags from Table 7, Formula-1 racing car event which occurred on Abu Dhabi was trending during the period. By varying input parameters (\(\alpha\) or \(\lambda\)), we discovered different sets of dense subgraphs. Note that often, due to simplicity, we give the set of hashtags with reference to the DCS solution which maximizes the sum of the densities. For example, if we set \(\alpha = 0.8\), only the indiangp tag is added to Day 1 to the DCS. On Day 5, tags teamlh and japanesegp are added to the dense hashtag collection which indicates that additional racing car event related tags are trending. For \(\alpha = 0.8\), the discovered density is 7.79 whereas letting \(\alpha = 0.5\) increases the density up to 9.63. To gain higher density, some of the tags sandracing, formula-1, wenevergiveup, and quali have newly added to several daily dense collections and abudhabi is removed from some of the dense collections but abudhabigp still remains in dense subgraphs. For the results of JWDS, we can observe more new tags like bahrain, english, arabic, french, danish, and swedish, which seem not directly related to the racing car event. Moreover, the new dense collection gives a higher density of 14.55. The larger value for JWDS is evident since JWDS finds solution sets without enforcing similarity constraints hardly as JCDS.

We see that discovered hashtags vary during 8 days. For example, let us consider the results of Hard. The tag indiangp was included only during Day 1, 2, and 5. However, abudhabigp tag was included over the entire period. The tags like teamlh and japanesegp were included only during Day 5.

DBLP dataset: The DBLP\(^2\) dataset contains annual co-authorship connections in top database and data mining conferences over a decade from 2006 to 2015. An edge corresponds to being a co-author pair in a publication. We pick authors who have co-authored papers at least in 4 different years during a period of 10 years. As shown in Table 8, we have uncovered a set of prominent co-authors. We see that the algorithms provide denser solutions than the densest common subgraph. For example, JCDS achieves a density of 21.86 while JWDS induces density value of 17.47 with score of 48.13.

We see that there is a temporal dependency of the collaborations between authors. The set found by Itr matches to the densest common subgraph only during 2010–2012, and during other years some authors are removed.

6 Concluding remarks

We introduced a Jaccard constrained, dense subgraph discovery problem (JCDS) for graphs with multiple snapshots. Here, our goal was to find a dense subset of vertices from each graph snapshot such that the sum of densities is maximized while pairwise Jaccard index constraint is satisfied.

We considered also an alternative problem (JWDS), where we incorporated Jaccard indices directly into the objective function, weighted by a user parameter.

We proved that both problems are NP-hard and hence we resorted to heuristics. First, we designed a greedy, iterative algorithm for JCDS which runs in \({\mathcal {O}} \mathopen {}\left( nk^2 + m\right)\) time, where k is the length of the graph sequence and n and m denote number of vertices and total number of edges respectively. For JWDS, we designed an iterative algorithm which runs in \({\mathcal {O}} \mathopen {}\left( n^2k^2 + m \log n + k^3 n\right)\) time per single iteration, and a greedy algorithm which runs in \({\mathcal {O}} \mathopen {}\left( n^2k^2 + m \log n + k^3 n\right)\) time.

We experimentally showed that the number of iterations was low in iterative algorithms, and that the algorithms could find the ground truth using synthetic datasets and could discover dense collections in real-world datasets. We also studied the effect of our user parameters: the threshold for the Jaccard index \(\alpha\) in JCDS, and weight \(\lambda\) for Jaccard indices in JWDS. Finally, we performed two case studies showing interpretable results.

The paper introduces several interesting directions for future work. In this paper, we enforced the pairwise Jaccard constraint between all available pairs of snapshots. However, we can relax this constraint further by letting only a portion of sets which lies within a specific window to assure the Jaccard similarity constraint which may lead to future work. Another possible direction is adopting different types of density for our problem setting.