Codeforces celebrates 10 years! We are pleased to announce the crowdfunding-campaign. Congratulate us by the link https://codeforces.com/10years. ×

Graph saving methods

Revision en1, by Arpa, 2017-03-19 20:34:14

Hi.

## Introduction.

We have a graph, we want to save it (and maybe run DFS on it for example); simple? Yes, but I want to show different methods for this, you can choose any of them depending on problem.

Simple, if graph is not weighted, save in G[v][u] if there is an edge between v and u or not.

If graph is weighted, if there is an edge between v and u save it’s weight in G[v][u], save  - 1 otherwise (if weight can be negative, use inf instead of  - 1).

Memory usage : .
Overall time complexity : (for running dfs).

Code here

### Vectors.

It's the most common method for saving graph. For each vertex keep a vector of it’s edges, now for each edge just save it in related vectors. Use pair (or another struct) for saving weighted graph. It works similar for directed graph.

Memory usage : (Note that vector uses more memory than you need).
Overall time complexity : (for running dfs).

Code here
##### Dynamic array.

Using dynamic arrays is better than saving in vector in some cases, because it uses less memory. For each vertex count edges connected to it at first, then allocate a enough space for each vertex to save it’s adjacency-list. Note that this method is Offline.

Memory usage : .
Overall time complexity : (for running dfs).

Code here

This method is used when id of edges are important and works only for directed graphs (or if you can convert the undirected graph to directed). We use array for saving edges. Then, for each vertex we keep index of last added edge and for each edge consider it’s from Fromi to Toi, we keep the index of last edge before this edge that Fromindex = Fromi and name it prvi. Then we can use headv and prv array to find adjacency-list.

Memory usage : .
Overall time complexity : (for running dfs).

Code here

Application: Solving flow network problems. Wikipedia : Maximum flow problem.

Naive implementation
Dinic’s blocking flow algorithm

Usage of this method in above codes are where we use cap[e] -= x, cap[e ^ 1] += x. Because of special adding edge method, it is guaranteed that if edge e is from v to u, is its pair and it’s from u to v.

Related submission : 23115110.

### Index keeping.

This method is used when index of edges is important (i.e. in queries we need to change something about edges, i.e. change weight, delete edge). Keep index of edges related to each vertex in its vector, and for each edge keep the vertices connecting with this edge (see code below).

Note that index keeping is possible and maybe easier using map (or unordered_map), but it is faster using this method. Also note that dynamic array method could be used here as well.

Memory usage : (Note that vector uses more memory than you need).
Overall time complexity : (for running dfs).

Code here

Example: Consider you need to change weight of edges online, this is simple using index keeping method, but it isn’t possible with other methods (at least it will be harder). Related submission : 20776118.

Application : Finding Euler tour. Wikipedia : Eulerian path. Blocking edges is easier with this method, that’s what we need in Euler tour finding. Related submissions : 21395473, 21636007.

## Comparison.

I used Codeforces polygon for comparing and generated 16 random graphs:

• #1 to #4 : n = 106, m = 106

• #5 to #4 : n = 105, m = 106

• #9 to #8 : n = 104, m = 106

• #13 to #16 : n = 103, m = 106

# dynamic array linked list vector
1 1606 / 44 1138 / 29 1575 / 39
2 1684 / 44 1153 / 29 1559 / 39
3 1575 / 44 1107 / 29 1496 / 39
4 1622 / 44 1122 / 29 1575 / 39
5 1575 / 44 1123 / 29 1543 / 39
6 1544 / 44 1122 / 29 1559 / 39
7 1590 / 44 1123 / 29 1575 / 39
8 1559 / 44 1154 / 29 1559 / 39
9 1622 / 44 1107 / 29 1621 / 39
10 1575 / 44 1169 / 29 1684 / 39
11 1591 / 44 1372 / 29 1575 / 39
12 1559 / 44 1091 / 29 1653 / 39
13 1559 / 44 1123 / 29 1684 / 39
14 1575 / 44 1169 / 29 1575 / 39
15 1575 / 44 1123 / 29 1559 / 39
16 1528 / 44 1123 / 29 1544 / 39
Σ 16 16 16
max. 1684ms / 44MB 1372ms / 29MB 1684ms / 39MB

It's really strange for me that vector uses less memory than dynamic array while testing, can someone explain the reason? I can share codes and generators in polygon if you want them.

As usual I’d like to finish the post with a poem:

بی سبب هرگز نمی‌گردد کسی یار کسی
یار بسیار است تا گرم است بازار کسی


By: Parto biza’ee arani.

Translation: No one becomes friend with another one without reason. People are like shops, a shop is full of customers while it has goods.

P.S. Kindly write in comments (or use private chat in case of necessary) if something is wrong.

#### History

Revisions

Rev. Lang. By When Δ Comment
en5 Arpa 2017-06-04 13:37:16 2 Fixed grammar mistakes using Grammarly.
en4 Arpa 2017-03-20 16:03:24 278 answer has been found
en3 Arpa 2017-03-20 12:01:34 3821 bug in generator fixed, statistics in comparison updated.
en2 Arpa 2017-03-19 22:42:20 114 Files added.
en1 Arpa 2017-03-19 20:34:14 40134 Initial revision (published)