SlideShare a Scribd company logo
Code In Python
File 1: main.py
You will implement two algorithms to find the shortest path in a graph.
1. def dijkstra(G,start_node): Takes an Adjacency Matrix G and the index of the starting
node start_node. Returns the distance array.
2. def floyd(G): Takes an Adjacency Matrix G and returns the distance matrix
The graph you are working on will be give in a file with the following format.
1. Line 1: The number of Nodes in the Graph
2. Lines 2-EOF: Every other line in the file contains an edge
1. First Value is the FROM node
2. Second Value is the TO node
3. Third Value is the weight of the edge
Note: You should store the weights as floats.
The program will have a command line interface. First ask for the file name of the graph to work
with. Then implement 4 text commands.
· dijkstra x - Runs Dijkstra starting at node X. X must be an integer
· floyd - Runs Floyd's algorithm
· help - prints this menu
· exit or ctrl-D - Exist the program
You must implement this using only standard python3 libraries. You may not use any outside
libraries. For example, open source graph libraries. Your code must run on tux with just main.py
and the input files. You may not include any other python files.
Example Run 1
File containing graph: input1.txt
Possible Commands are:
dijkstra x - Runs Dijkstra starting at node X. X must be an integer
floyd - Runs Floyd's algorithm
help - prints this menu
exit or ctrl-D - Exits the program
Enter command: help
Possible Commands are:
dijkstra x - Runs Dijkstra starting at node X. X must be an integer
floyd - Runs Floyd's algorithm
help - prints this menu
exit or ctrl-D - Exits the program
Enter command: dijkstra 0
[0.0, 1.0, 3.0, 5.0, 7.0]
Enter command: exit
Bye
Example Run 2
File containing graph: input1.txt
Possible Commands are:
dijkstra x - Runs Dijkstra starting at node X. X must be an integer
floyd - Runs Floyd's algorithm
help - prints this menu
exit or ctrl-D - Exits the program
Enter command: dijkstra 0
[0.0, 1.0, 3.0, 5.0, 7.0]
Enter command: dijkstra 1
[inf, 0.0, 2.0, 4.0, 6.0]
Enter command: dijkstra 2
[inf, 3.0, 0.0, 2.0, 4.0]
Enter command: dijkstra 3
[inf, 1.0, 3.0, 0.0, 7.0]
Enter command: dijkstra 4
[inf, 6.0, 8.0, 5.0, 0.0]
Enter command: exit
Bye
Example Run 3
File containing graph: input1.txt
Possible Commands are:
dijkstra x - Runs Dijkstra starting at node X. X must be an integer
floyd - Runs Floyd's algorithm
help - prints this menu
exit or ctrl-D - Exits the program
Enter command: floyd
[0.0, 1.0, 3.0, 5.0, 7.0]
[inf, 0.0, 2.0, 4.0, 6.0]
[inf, 3.0, 0.0, 2.0, 4.0]
[inf, 1.0, 3.0, 0.0, 7.0]
[inf, 6.0, 8.0, 5.0, 0.0]
Enter command: exit
Bye
Example Run 4
File containing graph: input2.txt
Possible Commands are:
dijkstra x - Runs Dijkstra starting at node X. X must be an integer
floyd - Runs Floyd's algorithm
help - prints this menu
exit or ctrl-D - Exits the program
Enter command: dijkstra 0
[0.0, 3.0, 4.0, 4.0, inf, 4.0]
Enter command: dijkstra 1
[inf, 0.0, 1.0, 3.0, inf, 1.0]
Enter command: dijkstra 2
[inf, 5.0, 0.0, 2.0, inf, 6.0]
Enter command: dijkstra 3
[inf, 3.0, 4.0, 0.0, inf, 4.0]
Enter command: dijkstra 4
[inf, 6.0, 7.0, 3.0, 0.0, 2.0]
Enter command: dijkstra 5
[inf, 5.0, 6.0, 2.0, inf, 0.0]
Enter command: floyd
[0.0, 3.0, 4.0, 4.0, inf, 4.0]
[inf, 0.0, 1.0, 3.0, inf, 1.0]
[inf, 5.0, 0.0, 2.0, inf, 6.0]
[inf, 3.0, 4.0, 0.0, inf, 4.0]
[inf, 6.0, 7.0, 3.0, 0.0, 2.0]
[inf, 5.0, 6.0, 2.0, inf, 0.0]
Enter command: exit
Bye
input1.txt
5
0 1 1
1 2 2
2 3 2
2 4 4
3 1 1
3 2 3
4 3 5
input2.txt
6
0 1 3
0 3 4
0 5 5
1 5 1
1 2 1
2 3 2
3 1 3
4 3 3
4 5 2
5 3 2
Solution
Interactive Mode Programming
Invoking the interpreter without passing a script file as a parameter brings up the following
prompt ?
Type the following text at the Python prompt and press the Enter:
If you are running older version of Python (Python 2.x), use of parenthesis as inprint function is
optional. This produces the following result:
Script Mode Programming
Invoking the interpreter with a script parameter begins execution of the script and continues until
the script is finished. When the script is finished, the interpreter is no longer active.
Let us write a simple Python program in a script. Python files have extension.py. Type the
following source code in a test.py file:
We assume that you have Python interpreter set in PATH variable. Now, try to run this program
as follows ?
On Linux
This produces the following result:
n Windows
This produces the following result:
Let us try another way to execute a Python script in Linux. Here is the modified test.py file ?
We assume that you have Python interpreter available in /usr/bin directory. Now, try to run this
program as follows ?
This produces the following result ?
Python Identifiers
A Python identifier is a name used to identify a variable, function, class, module or other object.
An identifier starts with a letter A to Z or a to z or an underscore (_) followed by zero or more
letters, underscores and digits (0 to 9).
Python does not allow punctuation characters such as @, $, and % within identifiers. Python is a
case sensitive programming language. Thus, Manpowerand manpower are two different
identifiers in Python.
Here are naming conventions for Python identifiers ?
Class names start with an uppercase letter. All other identifiers start with a lowercase letter.
Starting an identifier with a single leading underscore indicates that the identifier is private.
Starting an identifier with two leading underscores indicates a strongly private identifier.
If the identifier also ends with two trailing underscores, the identifier is a language-defined
special name.
Reserved Words
The following list shows the Python keywords. These are reserved words and you cannot use
them as constant or variable or any other identifier names. All the Python keywords contain
lowercase letters only.
Lines and Indentation
Python doesn't use braces({}) to indicate blocks of code for class and function definitions or
flow control. Blocks of code are denoted by line indentation, which is rigidly enforced.
The number of spaces in the indentation is variable, but all statements within the block must be
indented the same amount. For example ?
However, the following block generates an error ?
Thus, in Python all the continuous lines indented with same number of spaces would form a
block. The following example has various statement blocks ?
Note: Do not try to understand the logic at this point of time. Just make sure you understood
various blocks even if they are without braces.
Multi-Line Statements
Statements in Python typically end with a new line. Python does, however, allow the use of the
line continuation character () to denote that the line should continue. For example ?
Statements contained within the [], {}, or () brackets do not need to use the line continuation
character. For example ?
Quotation in Python
Python accepts single ('), double (") and triple (''' or """) quotes to denote string literals, as
long as the same type of quote starts and ends the string.
The triple quotes are used to span the string across multiple lines. For example, all the following
are legal ?
Comments in Python
A hash sign (#) that is not inside a string literal begins a comment. All characters after the # and
up to the end of the physical line are part of the comment and the Python interpreter ignores
them.
This produces the following result ?
You can type a comment on the same line after a statement or expression ?
Python doesn't have multiple-line commenting feature. You should comment each line
individually as follows ?
Using Blank Lines
A line containing only whitespace, possibly with a comment, is known as a blank line and
Python totally ignores it.
In an interactive interpreter session, you must enter an empty physical line to terminate a
multiline statement.
Waiting for the User
The following line of the program displays the prompt, the statement saying “Press the enter key
to exit”, and waits for the user to take action ?
Here, "  " is used to create two new lines before displaying the actual line. Once the user
presses the key, the program ends. This is a nice trick to keep a console window open until the
user is done with an application.
Multiple Statements on a Single Line
The semicolon ( ; ) allows multiple statements on the single line given that neither statement
starts a new code block. Here is a sample snip using the semicolon ?
Multiple Statement Groups as Suites
A group of individual statements, which make a single code block are calledsuites in Python.
Compound or complex statements, such as if, while, def, and class require a header line and a
suite.
Header lines begin the statement (with the keyword) and terminate with a colon ( : ) and are
followed by one or more lines which make up the suite. For example ?
Command Line Arguments
Many programs can be run to provide you with some basic information about how they should
be run. Python enables you to do this with -h ?
You can also program your script in such a way that it should accept various options. Command
Line Arguments is an advanced topic and should be studied a bit later once you have gone
through rest of the Python
concepts.andexecNotasfinallyorassertforpassbreakfromprintclassglobalraisecontinueifreturndefi
mporttrydelinwhileelifiswithelselambdayieldexcept

More Related Content

Similar to Code In PythonFile 1 main.pyYou will implement two algorithms t.pdf

lecture 2.pptx
lecture 2.pptxlecture 2.pptx
lecture 2.pptx
Anonymous9etQKwW
 
Python Session - 2
Python Session - 2Python Session - 2
Python Session - 2
AnirudhaGaikwad4
 
python ppt | Python Course In Ghaziabad | Scode Network Institute
python ppt | Python Course In Ghaziabad | Scode Network Institutepython ppt | Python Course In Ghaziabad | Scode Network Institute
python ppt | Python Course In Ghaziabad | Scode Network Institute
Scode Network Institute
 
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
DRVaibhavmeshram1
 
Python-01| Fundamentals
Python-01| FundamentalsPython-01| Fundamentals
Python-01| Fundamentals
Mohd Sajjad
 
Introduction To Python
Introduction To  PythonIntroduction To  Python
Introduction To Python
shailaja30
 
Spsl iv unit final
Spsl iv unit  finalSpsl iv unit  final
Spsl iv unit final
Sasidhar Kothuru
 
Spsl iv unit final
Spsl iv unit  finalSpsl iv unit  final
Spsl iv unit final
Sasidhar Kothuru
 
Python (3).pdf
Python (3).pdfPython (3).pdf
Python (3).pdf
samiwaris2
 
unit (1)INTRODUCTION TO PYTHON course.pptx
unit (1)INTRODUCTION TO PYTHON course.pptxunit (1)INTRODUCTION TO PYTHON course.pptx
unit (1)INTRODUCTION TO PYTHON course.pptx
usvirat1805
 
2022-23TYBSC(CS)-Python Prog._Chapter-1.pptx
2022-23TYBSC(CS)-Python Prog._Chapter-1.pptx2022-23TYBSC(CS)-Python Prog._Chapter-1.pptx
2022-23TYBSC(CS)-Python Prog._Chapter-1.pptx
sangeeta borde
 
Python (Data Analysis) cleaning and visualize
Python (Data Analysis) cleaning and visualizePython (Data Analysis) cleaning and visualize
Python (Data Analysis) cleaning and visualize
IruolagbePius
 
Looping in PythonLab8 lecture slides.pptx
Looping in PythonLab8 lecture slides.pptxLooping in PythonLab8 lecture slides.pptx
Looping in PythonLab8 lecture slides.pptx
adihartanto7
 
Python
PythonPython
CSC2308 - PRINCIPLE OF PROGRAMMING II.pdf
CSC2308 - PRINCIPLE OF PROGRAMMING II.pdfCSC2308 - PRINCIPLE OF PROGRAMMING II.pdf
CSC2308 - PRINCIPLE OF PROGRAMMING II.pdf
AbdulmalikAhmadLawan2
 
PPt Revision of the basics of python1.pptx
PPt Revision of the basics of python1.pptxPPt Revision of the basics of python1.pptx
PPt Revision of the basics of python1.pptx
tcsonline1222
 
Get started python programming part 1
Get started python programming   part 1Get started python programming   part 1
Get started python programming part 1
Nicholas I
 
Fundamentals of python
Fundamentals of pythonFundamentals of python
Fundamentals of python
BijuAugustian
 
Basic Concepts in Python
Basic Concepts in PythonBasic Concepts in Python
Basic Concepts in Python
Sumit Satam
 
WEB PROGRAMMING UNIT VIII BY BHAVSINGH MALOTH
WEB PROGRAMMING UNIT VIII BY BHAVSINGH MALOTHWEB PROGRAMMING UNIT VIII BY BHAVSINGH MALOTH
WEB PROGRAMMING UNIT VIII BY BHAVSINGH MALOTH
Bhavsingh Maloth
 

Similar to Code In PythonFile 1 main.pyYou will implement two algorithms t.pdf (20)

lecture 2.pptx
lecture 2.pptxlecture 2.pptx
lecture 2.pptx
 
Python Session - 2
Python Session - 2Python Session - 2
Python Session - 2
 
python ppt | Python Course In Ghaziabad | Scode Network Institute
python ppt | Python Course In Ghaziabad | Scode Network Institutepython ppt | Python Course In Ghaziabad | Scode Network Institute
python ppt | Python Course In Ghaziabad | Scode Network Institute
 
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
 
Python-01| Fundamentals
Python-01| FundamentalsPython-01| Fundamentals
Python-01| Fundamentals
 
Introduction To Python
Introduction To  PythonIntroduction To  Python
Introduction To Python
 
Spsl iv unit final
Spsl iv unit  finalSpsl iv unit  final
Spsl iv unit final
 
Spsl iv unit final
Spsl iv unit  finalSpsl iv unit  final
Spsl iv unit final
 
Python (3).pdf
Python (3).pdfPython (3).pdf
Python (3).pdf
 
unit (1)INTRODUCTION TO PYTHON course.pptx
unit (1)INTRODUCTION TO PYTHON course.pptxunit (1)INTRODUCTION TO PYTHON course.pptx
unit (1)INTRODUCTION TO PYTHON course.pptx
 
2022-23TYBSC(CS)-Python Prog._Chapter-1.pptx
2022-23TYBSC(CS)-Python Prog._Chapter-1.pptx2022-23TYBSC(CS)-Python Prog._Chapter-1.pptx
2022-23TYBSC(CS)-Python Prog._Chapter-1.pptx
 
Python (Data Analysis) cleaning and visualize
Python (Data Analysis) cleaning and visualizePython (Data Analysis) cleaning and visualize
Python (Data Analysis) cleaning and visualize
 
Looping in PythonLab8 lecture slides.pptx
Looping in PythonLab8 lecture slides.pptxLooping in PythonLab8 lecture slides.pptx
Looping in PythonLab8 lecture slides.pptx
 
Python
PythonPython
Python
 
CSC2308 - PRINCIPLE OF PROGRAMMING II.pdf
CSC2308 - PRINCIPLE OF PROGRAMMING II.pdfCSC2308 - PRINCIPLE OF PROGRAMMING II.pdf
CSC2308 - PRINCIPLE OF PROGRAMMING II.pdf
 
PPt Revision of the basics of python1.pptx
PPt Revision of the basics of python1.pptxPPt Revision of the basics of python1.pptx
PPt Revision of the basics of python1.pptx
 
Get started python programming part 1
Get started python programming   part 1Get started python programming   part 1
Get started python programming part 1
 
Fundamentals of python
Fundamentals of pythonFundamentals of python
Fundamentals of python
 
Basic Concepts in Python
Basic Concepts in PythonBasic Concepts in Python
Basic Concepts in Python
 
WEB PROGRAMMING UNIT VIII BY BHAVSINGH MALOTH
WEB PROGRAMMING UNIT VIII BY BHAVSINGH MALOTHWEB PROGRAMMING UNIT VIII BY BHAVSINGH MALOTH
WEB PROGRAMMING UNIT VIII BY BHAVSINGH MALOTH
 

More from aishwaryaequipment

Companies for your comparative analysis Caterpillar Inc. and Deere .pdf
Companies for your comparative analysis Caterpillar Inc. and Deere .pdfCompanies for your comparative analysis Caterpillar Inc. and Deere .pdf
Companies for your comparative analysis Caterpillar Inc. and Deere .pdf
aishwaryaequipment
 
Compano Inc. was founded in 1986 in Baytown, Texas. The firm provide.pdf
Compano Inc. was founded in 1986 in Baytown, Texas. The firm provide.pdfCompano Inc. was founded in 1986 in Baytown, Texas. The firm provide.pdf
Compano Inc. was founded in 1986 in Baytown, Texas. The firm provide.pdf
aishwaryaequipment
 
College students often make up a substantial portion of the populati.pdf
College students often make up a substantial portion of the populati.pdfCollege students often make up a substantial portion of the populati.pdf
College students often make up a substantial portion of the populati.pdf
aishwaryaequipment
 
coefficient of linear expansionSolutionThe change in length .pdf
coefficient of linear expansionSolutionThe change in length .pdfcoefficient of linear expansionSolutionThe change in length .pdf
coefficient of linear expansionSolutionThe change in length .pdf
aishwaryaequipment
 
Communications between computers can take several approaches. Which .pdf
Communications between computers can take several approaches. Which .pdfCommunications between computers can take several approaches. Which .pdf
Communications between computers can take several approaches. Which .pdf
aishwaryaequipment
 
Come up with a mean and standard deviation. Using the information yo.pdf
Come up with a mean and standard deviation. Using the information yo.pdfCome up with a mean and standard deviation. Using the information yo.pdf
Come up with a mean and standard deviation. Using the information yo.pdf
aishwaryaequipment
 
Complex AnalysisFind the image of (the region outside the unite ci.pdf
Complex AnalysisFind the image of (the region outside the unite ci.pdfComplex AnalysisFind the image of (the region outside the unite ci.pdf
Complex AnalysisFind the image of (the region outside the unite ci.pdf
aishwaryaequipment
 
completely forgot how to do these limits, could i have a extremely d.pdf
completely forgot how to do these limits, could i have a extremely d.pdfcompletely forgot how to do these limits, could i have a extremely d.pdf
completely forgot how to do these limits, could i have a extremely d.pdf
aishwaryaequipment
 
Complex logarithmDescribe log z.SolutionThe complex logarith.pdf
Complex logarithmDescribe log z.SolutionThe complex logarith.pdfComplex logarithmDescribe log z.SolutionThe complex logarith.pdf
Complex logarithmDescribe log z.SolutionThe complex logarith.pdf
aishwaryaequipment
 
Complete the Income Statement.What was the company’s interest expe.pdf
Complete the Income Statement.What was the company’s interest expe.pdfComplete the Income Statement.What was the company’s interest expe.pdf
Complete the Income Statement.What was the company’s interest expe.pdf
aishwaryaequipment
 
College Algebra Can anyone help me If $2500 is invested in the ac.pdf
College Algebra Can anyone help me If $2500 is invested in the ac.pdfCollege Algebra Can anyone help me If $2500 is invested in the ac.pdf
College Algebra Can anyone help me If $2500 is invested in the ac.pdf
aishwaryaequipment
 
Complete by hand. Show all work. 14. Information Gathering and E.pdf
Complete by hand. Show all work. 14. Information Gathering and E.pdfComplete by hand. Show all work. 14. Information Gathering and E.pdf
Complete by hand. Show all work. 14. Information Gathering and E.pdf
aishwaryaequipment
 
Compensation plans usually include a variety of benefits. Name the t.pdf
Compensation plans usually include a variety of benefits. Name the t.pdfCompensation plans usually include a variety of benefits. Name the t.pdf
Compensation plans usually include a variety of benefits. Name the t.pdf
aishwaryaequipment
 
Comparecontrast economies of scale and diseconomies of scale.So.pdf
Comparecontrast economies of scale and diseconomies of scale.So.pdfComparecontrast economies of scale and diseconomies of scale.So.pdf
Comparecontrast economies of scale and diseconomies of scale.So.pdf
aishwaryaequipment
 
Comparecontrast explicit and implicit costs.SolutionExplicit .pdf
Comparecontrast explicit and implicit costs.SolutionExplicit .pdfComparecontrast explicit and implicit costs.SolutionExplicit .pdf
Comparecontrast explicit and implicit costs.SolutionExplicit .pdf
aishwaryaequipment
 
Compare the Euro downfall with the other major currencies of the wor.pdf
Compare the Euro downfall with the other major currencies of the wor.pdfCompare the Euro downfall with the other major currencies of the wor.pdf
Compare the Euro downfall with the other major currencies of the wor.pdf
aishwaryaequipment
 
Compare the difference between using the RTI interrupt to control th.pdf
Compare the difference between using the RTI interrupt to control th.pdfCompare the difference between using the RTI interrupt to control th.pdf
Compare the difference between using the RTI interrupt to control th.pdf
aishwaryaequipment
 
compare and contrast the change interventions (first and second orde.pdf
compare and contrast the change interventions (first and second orde.pdfcompare and contrast the change interventions (first and second orde.pdf
compare and contrast the change interventions (first and second orde.pdf
aishwaryaequipment
 
Compare and contrast the national cultures of the U.S. and India usi.pdf
Compare and contrast the national cultures of the U.S. and India usi.pdfCompare and contrast the national cultures of the U.S. and India usi.pdf
Compare and contrast the national cultures of the U.S. and India usi.pdf
aishwaryaequipment
 
COMPARE AND CONTRAST TWO MAIN DIFFERENCES BETWEEN DOMESTIC AND INTER.pdf
COMPARE AND CONTRAST TWO MAIN DIFFERENCES BETWEEN DOMESTIC AND INTER.pdfCOMPARE AND CONTRAST TWO MAIN DIFFERENCES BETWEEN DOMESTIC AND INTER.pdf
COMPARE AND CONTRAST TWO MAIN DIFFERENCES BETWEEN DOMESTIC AND INTER.pdf
aishwaryaequipment
 

More from aishwaryaequipment (20)

Companies for your comparative analysis Caterpillar Inc. and Deere .pdf
Companies for your comparative analysis Caterpillar Inc. and Deere .pdfCompanies for your comparative analysis Caterpillar Inc. and Deere .pdf
Companies for your comparative analysis Caterpillar Inc. and Deere .pdf
 
Compano Inc. was founded in 1986 in Baytown, Texas. The firm provide.pdf
Compano Inc. was founded in 1986 in Baytown, Texas. The firm provide.pdfCompano Inc. was founded in 1986 in Baytown, Texas. The firm provide.pdf
Compano Inc. was founded in 1986 in Baytown, Texas. The firm provide.pdf
 
College students often make up a substantial portion of the populati.pdf
College students often make up a substantial portion of the populati.pdfCollege students often make up a substantial portion of the populati.pdf
College students often make up a substantial portion of the populati.pdf
 
coefficient of linear expansionSolutionThe change in length .pdf
coefficient of linear expansionSolutionThe change in length .pdfcoefficient of linear expansionSolutionThe change in length .pdf
coefficient of linear expansionSolutionThe change in length .pdf
 
Communications between computers can take several approaches. Which .pdf
Communications between computers can take several approaches. Which .pdfCommunications between computers can take several approaches. Which .pdf
Communications between computers can take several approaches. Which .pdf
 
Come up with a mean and standard deviation. Using the information yo.pdf
Come up with a mean and standard deviation. Using the information yo.pdfCome up with a mean and standard deviation. Using the information yo.pdf
Come up with a mean and standard deviation. Using the information yo.pdf
 
Complex AnalysisFind the image of (the region outside the unite ci.pdf
Complex AnalysisFind the image of (the region outside the unite ci.pdfComplex AnalysisFind the image of (the region outside the unite ci.pdf
Complex AnalysisFind the image of (the region outside the unite ci.pdf
 
completely forgot how to do these limits, could i have a extremely d.pdf
completely forgot how to do these limits, could i have a extremely d.pdfcompletely forgot how to do these limits, could i have a extremely d.pdf
completely forgot how to do these limits, could i have a extremely d.pdf
 
Complex logarithmDescribe log z.SolutionThe complex logarith.pdf
Complex logarithmDescribe log z.SolutionThe complex logarith.pdfComplex logarithmDescribe log z.SolutionThe complex logarith.pdf
Complex logarithmDescribe log z.SolutionThe complex logarith.pdf
 
Complete the Income Statement.What was the company’s interest expe.pdf
Complete the Income Statement.What was the company’s interest expe.pdfComplete the Income Statement.What was the company’s interest expe.pdf
Complete the Income Statement.What was the company’s interest expe.pdf
 
College Algebra Can anyone help me If $2500 is invested in the ac.pdf
College Algebra Can anyone help me If $2500 is invested in the ac.pdfCollege Algebra Can anyone help me If $2500 is invested in the ac.pdf
College Algebra Can anyone help me If $2500 is invested in the ac.pdf
 
Complete by hand. Show all work. 14. Information Gathering and E.pdf
Complete by hand. Show all work. 14. Information Gathering and E.pdfComplete by hand. Show all work. 14. Information Gathering and E.pdf
Complete by hand. Show all work. 14. Information Gathering and E.pdf
 
Compensation plans usually include a variety of benefits. Name the t.pdf
Compensation plans usually include a variety of benefits. Name the t.pdfCompensation plans usually include a variety of benefits. Name the t.pdf
Compensation plans usually include a variety of benefits. Name the t.pdf
 
Comparecontrast economies of scale and diseconomies of scale.So.pdf
Comparecontrast economies of scale and diseconomies of scale.So.pdfComparecontrast economies of scale and diseconomies of scale.So.pdf
Comparecontrast economies of scale and diseconomies of scale.So.pdf
 
Comparecontrast explicit and implicit costs.SolutionExplicit .pdf
Comparecontrast explicit and implicit costs.SolutionExplicit .pdfComparecontrast explicit and implicit costs.SolutionExplicit .pdf
Comparecontrast explicit and implicit costs.SolutionExplicit .pdf
 
Compare the Euro downfall with the other major currencies of the wor.pdf
Compare the Euro downfall with the other major currencies of the wor.pdfCompare the Euro downfall with the other major currencies of the wor.pdf
Compare the Euro downfall with the other major currencies of the wor.pdf
 
Compare the difference between using the RTI interrupt to control th.pdf
Compare the difference between using the RTI interrupt to control th.pdfCompare the difference between using the RTI interrupt to control th.pdf
Compare the difference between using the RTI interrupt to control th.pdf
 
compare and contrast the change interventions (first and second orde.pdf
compare and contrast the change interventions (first and second orde.pdfcompare and contrast the change interventions (first and second orde.pdf
compare and contrast the change interventions (first and second orde.pdf
 
Compare and contrast the national cultures of the U.S. and India usi.pdf
Compare and contrast the national cultures of the U.S. and India usi.pdfCompare and contrast the national cultures of the U.S. and India usi.pdf
Compare and contrast the national cultures of the U.S. and India usi.pdf
 
COMPARE AND CONTRAST TWO MAIN DIFFERENCES BETWEEN DOMESTIC AND INTER.pdf
COMPARE AND CONTRAST TWO MAIN DIFFERENCES BETWEEN DOMESTIC AND INTER.pdfCOMPARE AND CONTRAST TWO MAIN DIFFERENCES BETWEEN DOMESTIC AND INTER.pdf
COMPARE AND CONTRAST TWO MAIN DIFFERENCES BETWEEN DOMESTIC AND INTER.pdf
 

Recently uploaded

Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
TechSoup
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
iammrhaywood
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
Katrina Pritchard
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
NgcHiNguyn25
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
RAHUL
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
TechSoup
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
siemaillard
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
HajraNaeem15
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 

Recently uploaded (20)

Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 

Code In PythonFile 1 main.pyYou will implement two algorithms t.pdf

  • 1. Code In Python File 1: main.py You will implement two algorithms to find the shortest path in a graph. 1. def dijkstra(G,start_node): Takes an Adjacency Matrix G and the index of the starting node start_node. Returns the distance array. 2. def floyd(G): Takes an Adjacency Matrix G and returns the distance matrix The graph you are working on will be give in a file with the following format. 1. Line 1: The number of Nodes in the Graph 2. Lines 2-EOF: Every other line in the file contains an edge 1. First Value is the FROM node 2. Second Value is the TO node 3. Third Value is the weight of the edge Note: You should store the weights as floats. The program will have a command line interface. First ask for the file name of the graph to work with. Then implement 4 text commands. · dijkstra x - Runs Dijkstra starting at node X. X must be an integer · floyd - Runs Floyd's algorithm · help - prints this menu · exit or ctrl-D - Exist the program You must implement this using only standard python3 libraries. You may not use any outside libraries. For example, open source graph libraries. Your code must run on tux with just main.py and the input files. You may not include any other python files. Example Run 1 File containing graph: input1.txt Possible Commands are: dijkstra x - Runs Dijkstra starting at node X. X must be an integer floyd - Runs Floyd's algorithm help - prints this menu exit or ctrl-D - Exits the program Enter command: help Possible Commands are: dijkstra x - Runs Dijkstra starting at node X. X must be an integer floyd - Runs Floyd's algorithm help - prints this menu exit or ctrl-D - Exits the program
  • 2. Enter command: dijkstra 0 [0.0, 1.0, 3.0, 5.0, 7.0] Enter command: exit Bye Example Run 2 File containing graph: input1.txt Possible Commands are: dijkstra x - Runs Dijkstra starting at node X. X must be an integer floyd - Runs Floyd's algorithm help - prints this menu exit or ctrl-D - Exits the program Enter command: dijkstra 0 [0.0, 1.0, 3.0, 5.0, 7.0] Enter command: dijkstra 1 [inf, 0.0, 2.0, 4.0, 6.0] Enter command: dijkstra 2 [inf, 3.0, 0.0, 2.0, 4.0] Enter command: dijkstra 3 [inf, 1.0, 3.0, 0.0, 7.0] Enter command: dijkstra 4 [inf, 6.0, 8.0, 5.0, 0.0] Enter command: exit Bye Example Run 3 File containing graph: input1.txt Possible Commands are: dijkstra x - Runs Dijkstra starting at node X. X must be an integer floyd - Runs Floyd's algorithm help - prints this menu exit or ctrl-D - Exits the program Enter command: floyd [0.0, 1.0, 3.0, 5.0, 7.0] [inf, 0.0, 2.0, 4.0, 6.0] [inf, 3.0, 0.0, 2.0, 4.0] [inf, 1.0, 3.0, 0.0, 7.0] [inf, 6.0, 8.0, 5.0, 0.0]
  • 3. Enter command: exit Bye Example Run 4 File containing graph: input2.txt Possible Commands are: dijkstra x - Runs Dijkstra starting at node X. X must be an integer floyd - Runs Floyd's algorithm help - prints this menu exit or ctrl-D - Exits the program Enter command: dijkstra 0 [0.0, 3.0, 4.0, 4.0, inf, 4.0] Enter command: dijkstra 1 [inf, 0.0, 1.0, 3.0, inf, 1.0] Enter command: dijkstra 2 [inf, 5.0, 0.0, 2.0, inf, 6.0] Enter command: dijkstra 3 [inf, 3.0, 4.0, 0.0, inf, 4.0] Enter command: dijkstra 4 [inf, 6.0, 7.0, 3.0, 0.0, 2.0] Enter command: dijkstra 5 [inf, 5.0, 6.0, 2.0, inf, 0.0] Enter command: floyd [0.0, 3.0, 4.0, 4.0, inf, 4.0] [inf, 0.0, 1.0, 3.0, inf, 1.0] [inf, 5.0, 0.0, 2.0, inf, 6.0] [inf, 3.0, 4.0, 0.0, inf, 4.0] [inf, 6.0, 7.0, 3.0, 0.0, 2.0] [inf, 5.0, 6.0, 2.0, inf, 0.0] Enter command: exit Bye input1.txt 5 0 1 1 1 2 2 2 3 2 2 4 4
  • 4. 3 1 1 3 2 3 4 3 5 input2.txt 6 0 1 3 0 3 4 0 5 5 1 5 1 1 2 1 2 3 2 3 1 3 4 3 3 4 5 2 5 3 2 Solution Interactive Mode Programming Invoking the interpreter without passing a script file as a parameter brings up the following prompt ? Type the following text at the Python prompt and press the Enter: If you are running older version of Python (Python 2.x), use of parenthesis as inprint function is optional. This produces the following result: Script Mode Programming Invoking the interpreter with a script parameter begins execution of the script and continues until the script is finished. When the script is finished, the interpreter is no longer active. Let us write a simple Python program in a script. Python files have extension.py. Type the following source code in a test.py file: We assume that you have Python interpreter set in PATH variable. Now, try to run this program as follows ? On Linux This produces the following result: n Windows This produces the following result: Let us try another way to execute a Python script in Linux. Here is the modified test.py file ?
  • 5. We assume that you have Python interpreter available in /usr/bin directory. Now, try to run this program as follows ? This produces the following result ? Python Identifiers A Python identifier is a name used to identify a variable, function, class, module or other object. An identifier starts with a letter A to Z or a to z or an underscore (_) followed by zero or more letters, underscores and digits (0 to 9). Python does not allow punctuation characters such as @, $, and % within identifiers. Python is a case sensitive programming language. Thus, Manpowerand manpower are two different identifiers in Python. Here are naming conventions for Python identifiers ? Class names start with an uppercase letter. All other identifiers start with a lowercase letter. Starting an identifier with a single leading underscore indicates that the identifier is private. Starting an identifier with two leading underscores indicates a strongly private identifier. If the identifier also ends with two trailing underscores, the identifier is a language-defined special name. Reserved Words The following list shows the Python keywords. These are reserved words and you cannot use them as constant or variable or any other identifier names. All the Python keywords contain lowercase letters only. Lines and Indentation Python doesn't use braces({}) to indicate blocks of code for class and function definitions or flow control. Blocks of code are denoted by line indentation, which is rigidly enforced. The number of spaces in the indentation is variable, but all statements within the block must be indented the same amount. For example ? However, the following block generates an error ? Thus, in Python all the continuous lines indented with same number of spaces would form a block. The following example has various statement blocks ? Note: Do not try to understand the logic at this point of time. Just make sure you understood various blocks even if they are without braces. Multi-Line Statements Statements in Python typically end with a new line. Python does, however, allow the use of the line continuation character () to denote that the line should continue. For example ? Statements contained within the [], {}, or () brackets do not need to use the line continuation character. For example ?
  • 6. Quotation in Python Python accepts single ('), double (") and triple (''' or """) quotes to denote string literals, as long as the same type of quote starts and ends the string. The triple quotes are used to span the string across multiple lines. For example, all the following are legal ? Comments in Python A hash sign (#) that is not inside a string literal begins a comment. All characters after the # and up to the end of the physical line are part of the comment and the Python interpreter ignores them. This produces the following result ? You can type a comment on the same line after a statement or expression ? Python doesn't have multiple-line commenting feature. You should comment each line individually as follows ? Using Blank Lines A line containing only whitespace, possibly with a comment, is known as a blank line and Python totally ignores it. In an interactive interpreter session, you must enter an empty physical line to terminate a multiline statement. Waiting for the User The following line of the program displays the prompt, the statement saying “Press the enter key to exit”, and waits for the user to take action ? Here, " " is used to create two new lines before displaying the actual line. Once the user presses the key, the program ends. This is a nice trick to keep a console window open until the user is done with an application. Multiple Statements on a Single Line The semicolon ( ; ) allows multiple statements on the single line given that neither statement starts a new code block. Here is a sample snip using the semicolon ? Multiple Statement Groups as Suites A group of individual statements, which make a single code block are calledsuites in Python. Compound or complex statements, such as if, while, def, and class require a header line and a suite. Header lines begin the statement (with the keyword) and terminate with a colon ( : ) and are followed by one or more lines which make up the suite. For example ? Command Line Arguments Many programs can be run to provide you with some basic information about how they should be run. Python enables you to do this with -h ?
  • 7. You can also program your script in such a way that it should accept various options. Command Line Arguments is an advanced topic and should be studied a bit later once you have gone through rest of the Python concepts.andexecNotasfinallyorassertforpassbreakfromprintclassglobalraisecontinueifreturndefi mporttrydelinwhileelifiswithelselambdayieldexcept