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 6689589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:32:14+00:00 2026-05-26T05:32:14+00:00

I have a graph like this: As part of a homework assignment I want

  • 0

I have a graph like this:

graph

As part of a homework assignment I want to find the triangle (1->2->5). I have no idea how to find this.

In my case, I defined my graph:

type Graph = (Int, Int -> Int -> Bool)

g 2 3 = True
g 3 2 = True
g 1 2 = True
g 2 1 = True
g 1 1 = True
g n m = False

Answer to 2 comment.

I did this and it works, I think.

triangles :: [(Int, Int, Int)]
triangles = [(x, y, z) | x <- [1..3], y <- [1..x], z <- [1..y], isTriangle (x, y, z)]

isTriangle :: (Int, Int, Int) -> Bool
isTriangle  (x, y, z) = g x y && g y z && g x z

I removed (_,g) and (n,g) (I dont understand why we need them 🙂
I call trinagles and it return (1,1,1) (2,1,1) (in my case). Is it right?

  • 1 1 Answer
  • 1 View
  • 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-05-26T05:32:14+00:00Added an answer on May 26, 2026 at 5:32 am

    I guess the first Int of Graph is a bound for your nodes (like, 6 if the nodes are in [1..6]).

    Therefore, you would like a function that returns the triangles of a graph, so the type might be:

    triangles :: Graph -> [(Int, Int, Int)]
    

    Now, a triangle exists whenever, for 3 nodes, say x y and z, all the combinations return True through g.

    So, you might want to consider generating all these combinations (possibly avoiding the ones that are equivalent via re-ordering), and filter out only those that validate the criterion:

    isTriangle :: Graph -> (Int, Int, Int) -> Bool
    isTriangle (_, g) (x, y, z) == g x y && g y z && g x z
    

    For this, you could use a list comprehension, or the function filter which has type (a -> Bool) -> [a] -> [a]


    Answer to your first comment:

    First, you would need to implement the triangles function, which is the reason of the error. But, as you have done in test, you could simply generate these triangles on the fly.

    Now, you wrote:

    test = filter (isTriangle) [(x,y,z) | x <- [1..3], y <- [1..3], z <- [1..3]]
    

    Two things about this:

    • First, you wouldn’t need the parentheses around isTriangle for what you wrote, but it is incorrect, since isTriangle expects a graph as its first parameter
    • Second, you are going to obtain a lot of duplicates, and if you want, you can prevent this by not generating them in the first place:

      test = filter (isTriangle) [(x,y,z) | x <- [1..3], y <- [1..x], z <- [1..y]]
      

    Alternatively, you can dismiss the filter function by providing a guard in the list comprehension syntax, as this:

    [(x, y, z) | x <- [1..3],  y <- [1..x], z <- [1..y], isTriangle yourGraph (x, y, z)]
    

    Now, I’ll let you go on with the details. You will want to make this a function that takes a graph, and to replace this 3 by the number of nodes in the graph, and yourGraph by said graph.

    Since you chose to use list comprehension, forget about the generating function that I wrote about earlier, its purpose was just to generate input for filter, but with the list comprehension approach you won’t necessarily need it.


    Answer to your second comment:

    You want to write a function:

    triangles :: Graph -> [(Int, Int, Int)]
    triangles (n, g) = [(x, y, z) | ...]
    

    The ... are to be replaced with the correct things, from earlier (ranges for x, y and z, as well as the predicate isTriangle).

    Alternatively, you can cut this in two functions:

    allTriangles :: Int -> [(Int, Int, Int)]
    allTriangles n = [(x, y, z) | ...]
    
    graphTriangles :: Graph -> [(Int, Int, Int)]
    graphTriangles (n, g) = [t | t <- allTriangles n, isGraphTriangle t]
        where isGraphTriangle (x, y, z) = ...
    

    This way, you could potentially reuse allTriangles for something else. If you don’t feel the need, you can stay with the one-shot big comprehension triangles, since it’s a homework you probably won’t build up on it.

    I try not to fill all the ... so that you can do it yourself and hopefully understand 🙂


    Correcting your solution:

    First, my mistake on the ranges, it should be x <- [1..n], y <- [x+1..n], z <- [y+1..n] where n denotes the number of nodes in your graph. This way, you only capture triples where x < y < z, which ensures that you only see one occurence of each set of three points.

    Second, the reason why I put the graph as a parameter to the functions is that you might want to reuse the same function for another graph. By hardcoding g and 6 in your functions, you make them really specific to the particular graph you described, but if you want to compute triangles on a certain number of graphs, you do not want to write one function per graph!

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

Sidebar

Related Questions

I have a question. I have plotted a graph using Matplotlib like this: from
I have to represent graphically an oriented graph like in the image below. alt
I have a graph created with MS Chart like the following picture. As you
I have an undirected graph on matris by vertex adjacency relations like that; /*
Let's say I have an object graph of Countries / States / Cities, like
I have a graph (organigram) how this: digraph G { nodesep=0.3; ranksep=0.2; margin=0.1; node
Possible Duplicate: Assignment from incompatible pointer type warning I have two structures, one which
I have an object model that looks like this (pseudo code): class Product {
I have a <div> that displays a graph inside. Sometimes this graph gets too
I have implemented graphs using this tutorial http://www.raywenderlich.com/13271/how-to-draw-graphs-with-core-plot-part-2 now i want to add a

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.