SlideShare a Scribd company logo
1 of 7
Download to read offline
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

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 InstituteScode 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| FundamentalsMohd Sajjad
 
Introduction To Python
Introduction To  PythonIntroduction To  Python
Introduction To Pythonshailaja30
 
Python (3).pdf
Python (3).pdfPython (3).pdf
Python (3).pdfsamiwaris2
 
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.pptxusvirat1805
 
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.pptxsangeeta borde
 
Python (Data Analysis) cleaning and visualize
Python (Data Analysis) cleaning and visualizePython (Data Analysis) cleaning and visualize
Python (Data Analysis) cleaning and visualizeIruolagbePius
 
Looping in PythonLab8 lecture slides.pptx
Looping in PythonLab8 lecture slides.pptxLooping in PythonLab8 lecture slides.pptx
Looping in PythonLab8 lecture slides.pptxadihartanto7
 
CSC2308 - PRINCIPLE OF PROGRAMMING II.pdf
CSC2308 - PRINCIPLE OF PROGRAMMING II.pdfCSC2308 - PRINCIPLE OF PROGRAMMING II.pdf
CSC2308 - PRINCIPLE OF PROGRAMMING II.pdfAbdulmalikAhmadLawan2
 
Get started python programming part 1
Get started python programming   part 1Get started python programming   part 1
Get started python programming part 1Nicholas I
 
Fundamentals of python
Fundamentals of pythonFundamentals of python
Fundamentals of pythonBijuAugustian
 
Basic Concepts in Python
Basic Concepts in PythonBasic Concepts in Python
Basic Concepts in PythonSumit 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 MALOTHBhavsingh Maloth
 
1. python programming
1. python programming1. python programming
1. python programmingsreeLekha51
 

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

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
 
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
 
Python 3.pptx
Python 3.pptxPython 3.pptx
Python 3.pptx
 
1. python programming
1. python programming1. python programming
1. python programming
 

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 .pdfaishwaryaequipment
 
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.pdfaishwaryaequipment
 
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.pdfaishwaryaequipment
 
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 .pdfaishwaryaequipment
 
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 .pdfaishwaryaequipment
 
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.pdfaishwaryaequipment
 
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.pdfaishwaryaequipment
 
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.pdfaishwaryaequipment
 
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.pdfaishwaryaequipment
 
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.pdfaishwaryaequipment
 
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.pdfaishwaryaequipment
 
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.pdfaishwaryaequipment
 
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.pdfaishwaryaequipment
 
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.pdfaishwaryaequipment
 
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 .pdfaishwaryaequipment
 
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.pdfaishwaryaequipment
 
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.pdfaishwaryaequipment
 
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.pdfaishwaryaequipment
 
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.pdfaishwaryaequipment
 
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.pdfaishwaryaequipment
 

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

Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonJericReyAuditor
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 

Recently uploaded (20)

Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lesson
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 

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