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

  • Home
  • SEARCH
  • 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 3939446
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:18:04+00:00 2026-05-20T00:18:04+00:00

I need to implement Dijkstra’s Algorithm in Python. However, I have to use a

  • 0

I need to implement Dijkstra’s Algorithm in Python. However, I have to use a 2D array to hold three pieces of information – predecessor, length and unvisited/visited.
I know in C a Struct can be used, though I am stuck on how I can do a similar thing in Python, I am told it’s possible but I have no idea to be honest

  • 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-20T00:18:04+00:00Added an answer on May 20, 2026 at 12:18 am

    As mentioned above, you can use an instance of an object.

    This author has a pretty convincing python implementation of Dijkstras in python.

    #
    # This file contains the Python code from Program 16.16 of
    # "Data Structures and Algorithms
    # with Object-Oriented Design Patterns in Python"
    # by Bruno R. Preiss.
    #
    # Copyright (c) 2003 by Bruno R. Preiss, P.Eng.  All rights reserved.
    #
    # http://www.brpreiss.com/books/opus7/programs/pgm16_16.txt
    #
    class Algorithms(object):
    
        def DijkstrasAlgorithm(g, s):
            n = g.numberOfVertices
            table = Array(n)
            for v in xrange(n):
                table[v] = Algorithms.Entry()
            table[s].distance = 0
            queue = BinaryHeap(g.numberOfEdges)
            queue.enqueue(Association(0, g[s]))
            while not queue.isEmpty:
                assoc = queue.dequeueMin()
                v0 = assoc.value
                if not table[v0.number].known:
                    table[v0.number].known = True
                    for e in v0.emanatingEdges:
                        v1 = e.mateOf(v0)
                        d = table[v0.number].distance + e.weight
                        if table[v1.number].distance > d:
    
                            table[v1.number].distance = d
                            table[v1.number].predecessor = v0.number
                            queue.enqueue(Association(d, v1))
            result = DigraphAsLists(n)
            for v in xrange(n):
                result.addVertex(v, table[v].distance)
            for v in xrange(n):
                if v != s:
                    result.addEdge(v, table[v].predecessor)
            return result
        DijkstrasAlgorithm = staticmethod(DijkstrasAlgorithm)
    

    Notice those pieces of information are ‘held’ in the object he is constructing by calling Algorithms.Entry(). Entry is a class and is defined like this:

    class Entry(object):
        """
        Data structure used in Dijkstra's and Prim's algorithms.
        """
    
        def __init__(self):
            """
            (Algorithms.Entry) -> None
            Constructor.
            """
            self.known = False
            self.distance = sys.maxint
            self.predecessor = sys.maxint
    

    The self.known, self.distance… are those pieces of information. He does not set these explicit in the constructor (init) but sets them later. In Python you can access attributes with dot notation. for examle: myObject= Entry(). the myObject.known, myObject.distance… they are all public.

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

Sidebar

Related Questions

I need to use (not implement) an array based version of Dijkstras algo .The
I need to implement a Diff algorithm in VB.NET to find the changes between
Hi I need implement context help inside my .NET application. I have .chm file
I need to implement a B-Tree for University: I have an outer class B-Tree
I need to implement version control, even for just the developing I do at
I need to implement a 4-to-1 function in Veriog. The input is 4 bits,
I need to implement auto-capitalization inside of a Telerik RadEditor control on an ASPX
I need to implement an in-memory tuple-of-strings matching feature in C. There will be
We need to implement a simple state machine in C . Is a standard
I need to implement an OpenID Provider in .Net and wondered....Is there's any OpenSource

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.