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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:12:06+00:00 2026-05-15T16:12:06+00:00

I have some simple code that represents a graph using a square boolean matrix

  • 0

I have some simple code that represents a graph using a square boolean matrix where rows/columns are nodes and true represents an undirected link between two nodes. I am initializing this matrix with False values and then setting the value to True where a link exists.

I believe the way I am initializing the list is causing a single bool instance to be referenced by each cell in a given row. The result is that if I set any cell to True all the other cells in that row also become True.

How should I be initializing my square matrix so that all values are false but none are shared by reference with other cells?

import sys

class Graph(object):
    def __init__(self, nodeCount, links):
        self.matrix = [[False] * nodeCount] * nodeCount
        for l in links:
            self.matrix[l[0]][l[1]] = True

    def __str__(self):
        s = "  "
        for i in range(len(self.matrix)):
            s += str(i) + " "
        s += "\n"
        for r in range(len(self.matrix)):
            s += str(r) + " "
            for c in range(len(self.matrix)):
                s += str(self.matrix[c][r])[0] + " "
            s += "\n"
        return s

g = Graph(5, [(2,3)])
print g

Also, on GIST

  • 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-05-15T16:12:07+00:00Added an answer on May 15, 2026 at 4:12 pm

    Actually, you’ve slightly misunderstood the problem. You believe that the boolean references are shared (this is true, but not important– booleans are immutable, so sharing references to the same object doesn’t mean much). What’s happened is that the list references are shared, and that’s caused your troubles. Let me show you:

    Your code is this

    [[False] * nodeCount] * nodeCount
    

    What happens is that you get nodeCount references to a single list with nodeCount references to False. Multiplying a sequene by an integer gives you a sequence with duplicated references– they’re not copies, they’re aliases.

    >>> x = [False] * 3
    >>> y = [x] * 3
    >>> y[0] is y[1]
    True
    >> # your problem
    >>> y[0][0] = True
    >>> y[1]
    [True, False, False]
    

    So in this case, it means you can’t change individual rows, because all the rows are the same list, and changing one row changes all.

    to fix this, make new lists for each row:

    [[False]*nodeCount for _ in xrange(nodeCount)]
    

    example:

    >>> y = [[False]*3 for _ in xrange(3)]
    >>> y[0] is y[1]
    False
    >>> y[0][0] = True
    >>> y[1]
    [False, False, False]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 471k
  • Answers 471k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer views-view.list.tpl.php. does the whole list not a node, are you… May 16, 2026 at 3:18 am
  • Editorial Team
    Editorial Team added an answer You can trigger the JIT compiler to compile your entire… May 16, 2026 at 3:18 am
  • Editorial Team
    Editorial Team added an answer pydoc is actually a Python script (so, on Windows, you… May 16, 2026 at 3:18 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.