Why does this algorithm work in O(n m)?

This is from a blog post on Codeforces. I couldn’t really understand why the editorialist goes on to claim that this code works in O(n m)

This is a graph problem, where we are supposed to find the number of ways to traverse from a to c.
Note that only the paths like a–>b–>c and a–>d–>c need to be considered. That is, there should be a difference of only one node, in between them.

n is the number of vertices, m is the number of edges and nxt is the adjacency list.

Let’s iterate through all combinations of a and c just two simple
nested loops in O(n^2) and find all candidates for b and d inside. To
find candidates you can go through all neighbors of a and check that
they are neighbors of c. Among all the candidates you should choose
two junctions as b and d. So just use
https://en.wikipedia.org/wiki/Combination All you need is to add to
the answer , where r is the number of candidates (common neighbors of
a and c). The code is:

for (int a = 0; a < n; a++)
    for (int c = 0; c < n; c++)
        if (a != c)
        {
            int r = 0;
            for (int b = 0; b < nxt[a].size(); b++)
                if (nxt[a][b] != a && nxt[a][b] != c && g[nxt[a][b]][c])
                    r++;
            result += r * (r - 1) / 2;
        }

It is easy to see that the total complexity is O(nm), because of sum of number of neighbors over all junctions is exactly m.

If there are 3 loops involved, then how can the worst case complexity be O(n m)

4

The outer loop has n iterations.

The middle loop has n iterations.

The inner loop has deg(a) iterations. This is because the nxt array is adjacency list of the graph, which means nxt[a] is a list of all edges going from vertex a and therefore nxt[a].size() is (out) degree of a.

Because m, the number of edges, is ∑a deg(a), the inner two loops together make m iterations and times the outer n is the result.


Notes: The test whether edge from nxt[a][b] to c exists is O(1), because g is incidence matrix. It is a bit unusual to see both adjacency list and incidence graph representations, because they both have to be prepared. The usual approach is to use adjacency list for sparse graphs, i.e. those with m ~ O(n), because then deg(a) ~ O(1) and linear search in the for the follow-up edge is constant-time, and to use incidence matrix for dense graphs, i.e. those with m ~O(n2), because then the O(n m) = O(n3) anyway, so looping over all vertices instead of out edges in the inner loop does not matter.

The other issue is that the author of this should be condemned for the choice of variables. It is usual for mathematical algorithms to use single-letter variables, but at least variable indicating index into the adjacency list should not be taken from the same range as variables for vertices. I’d use customary u and v for vertices (that is u = a and v = c) and i for index (that is i = b).

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật