Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7807265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:48:02+00:00 2026-06-02T02:48:02+00:00

Here is an exercise in the Algorithm Design Manual . Consider the problem of

  • 0

Here is an exercise in the Algorithm Design Manual.

Consider the problem of determining whether a given undirected graph G
= (V, E) contains a triangle or cycle of length 3.

(a) Give an O(|V |^3) to find a triangle if one exists.

(b) Improve
your algorithm to run in time O(|V |·|E|). You may assume |V | ≤ |E|.

Observe that these bounds gives you time to convert between the
adjacency matrix and adjacency list representations of G.

Here is my thoughts:

(a) If the graph is given as an adjacency list, I can convert the list to matrix by O(|V|^2). then I do:

for (int i = 0;i < n;i++) 
   for (int j = i+1;j < n;j++) 
   if (matrix[i][j] == 1) 
      for (int k = j+1;k < n;k++) 
         if (matrix[i][k] == 1 && matrix[j][k] == 1)
             return true;

This should give a O(|V|^3) to test the triangle.

(b) My first intuitive is that if the graph is given as an adjacency list, then I will do a bfs. Whenever a cross edge is found, for example, if y-x is a cross edge, then i will check whether parent[y] == parent[x], if true, then a triangle is found.

Could anyone tell me whether my thinking is correct or not?

Also for this (b), I am not sure its complexity. Should it be O(|V| + |E|), right?

How can I do it in O(|V|*|E|)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T02:48:05+00:00Added an answer on June 2, 2026 at 2:48 am

    A possible O(|V||E|) solution, is the same idea of the brute-force in (a):

    for each edge (u, v):
      for each vertex w:
         if (v, w) is an edge and (w, u) is an edge:
              return true
    return false
    

    check all edges, and not all vertices pairs – with another vertex that forms a triangle – it is enough information to determine if the edge and vertex form a feasible solution.

    Counter example to BFS solution:

           A
         / | \
        /  |  \
       B   C   D
       |   |   |
       |   |   |
       F---G---H
       |       |
       ---------
        (F, H) is also an edge
    

    Note that father[F] != father[G] != father[H], thus the algorithm will return false – but nevertheless, (F, G, H) is a feasible solution!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is an exercise (3-15) in the book Algorithm Design Manual. Design a data
Here's my proposed (very simplified to illustrate the problem space) design for a C#
I am reading the Algorithm Design Manual Second Edition and this is from an
The median of five is sometimes used as an exercise in algorithm design and
I am hunting job now and doing many algorithm exercises. Here is my problem:
I'm having trouble with Exercise 4 in Chapter 7 of railstutorial.org . Here are
I want to write a simple web proxy, for exercise. Here's the code I
Here's what I want to do: Given a table PeopleOutfit (id int primary key,
I'm trying to improve my current algorithm for the 8 Queens problem, and this
The Problem: Exercise 2-8 of The C Programming Language, Write a function rightrot(x,n) that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.