Python Forum
Python - networkx - Triangle inequality - Graph
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python - networkx - Triangle inequality - Graph
#1
Given this code, answer the following questions:

import networkx as nx

def function_x(G)
  distances = nx.all_pairs_dijkstra_path_length(G, weight='weight')
  count = 0
  for u, ud in distances:
    for v, d in ud.items():
      if G.has_edge(u, v):
        if G.edges[u, v]['weight'] > d:
          count += 1
          G.edges[u, v]['weight'] = d
  return count
Obviously G stands for a graph with positive weights.

a) What does this code do and what does it return?

b) Given that this algorithm can change the weights of some edges, is it possible that the distances that have been calculated before with the function all_pairs_dijkstra_path_length to change when function_x() exits?

For the first question, I think that this code "secures" the triangle inequality by putting a max limit (variable d) for the weights. If I am not mistaken this code will return the number of times that triangle inequality was violated in the graph (variable count).
As for the second question I have literally no clue!
What do you think of these questions?

P.S: 1) This is my first post and I am a beginner programmer.
2) Sorry for my bad english.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to write a recursion syntax for Sierpinski triangle using numpy? Bolzano 2 3,853 Apr-03-2021, 06:11 AM
Last Post: SheeppOSU
  Print user input into triangle djtjhokie 1 2,370 Nov-07-2020, 07:01 PM
Last Post: buran
  Tkinter - The Reuleaux Triangle andrewapk 1 1,937 Oct-06-2020, 09:01 PM
Last Post: deanhystad
  networkx code help zahermoh 1 2,075 May-25-2020, 07:16 AM
Last Post: pyzyx3qwerty
  Triangle function program m8jorp8yne 2 8,862 Dec-13-2019, 05:24 PM
Last Post: Clunk_Head
  Print triangle using while loop tuxandrew 3 4,915 Dec-05-2019, 07:17 PM
Last Post: micseydel
  Intersection of a triangle and a circle Gira 3 3,592 May-19-2019, 06:04 PM
Last Post: heiner55
  Draw isosceles triangle fen1c5 4 12,836 Jul-07-2018, 10:20 AM
Last Post: fen1c5
  Pascal's triangle nvakada 5 4,603 May-01-2018, 02:44 AM
Last Post: Skaperen
  Object oriented area of a triangle xterakojede 2 8,930 Apr-20-2018, 01:42 PM
Last Post: xterakojede

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020