我正在尝试编写Dijkstra的算法,但我正在努力解决如何在代码中"说出"某些事情.为了可视化,这里是我想要使用数组表示的列:
max_nodes
A B C Length Predecessor Visited/Unvisited
A 0 1 2 -1 U
B 1 0 1 -1 U
C 2 1 0 -1 U
Run Code Online (Sandbox Code Playgroud)
所以,会有几个数组,如下面的代码所示:
def dijkstra (graph, start, end)
network[max_nodes][max_nodes]
state [max_nodes][length]
state2 [max_nodes][predecessor]
state3 [max_nodes][visited]
initialNode = 0
for nodes in graph:
D[max_nodes][length] = -1
P[max_nodes][predecessor] = ""
V[max_nodes][visited] = false
for l in graph:
length = lengthFromSource[node] + graph[node][l]
if length < lengthFromSourceNode[w]:
state[l][length] = x
state2[l][predecessor]
state3[l][visited] = true
x +=1
Run Code Online (Sandbox Code Playgroud)
粗体部分是我坚持的地方 - 我正在尝试实现算法的这一部分: …
我收到错误'对象引用没有设置为对象的实例'.我试过看类似的问题,但真的看不出我的程序有什么问题.我遇到错误的代码行是:
labelQuestion.Text = table.Rows[0]["Question"].ToString();
Run Code Online (Sandbox Code Playgroud)
这是我的完整代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data.Sql;
using System.Data.SqlClient;
namespace Quiz_Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
String chosenAnswer, correctAnswer;
DataTable table;
private void Form1_Load(object sender, EventArgs e)
{
//declare connection string using windows security
string cnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Hannah\\Desktop\\QuizQuestions.accdb";
//declare Connection, command and other related objects
OleDbConnection conGet = new OleDbConnection(cnString);
OleDbCommand cmdGet = …Run Code Online (Sandbox Code Playgroud) 我的代码有两个问题 - 我不知道如何将我的populateArray函数与我的main函数链接; 我不确定我需要传递什么参数另外,我一直在打开要打开的文件的文件路径 - 路径是正确的,文件存在数据.这是我的代码:
network = []
def populateArray():
file = open('theroute.txt', 'r')
network = []
for line in file:
network.append(line)
print "Network = "
print network
file.close()
def main():
if __name__ == "__main__":
populateArray()
Run Code Online (Sandbox Code Playgroud)
任何帮助,将不胜感激!
感谢您的回复 - 我的代码现在看起来像上面,但当我删除def main()时:我收到以下错误:
File "populateArray.py", line 18
if __name__ == "__main__":
^
IndentationError: unindent does not match any outer indentation level
Run Code Online (Sandbox Code Playgroud)