Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
115 views

Python Programs-Part 2-Graph Theory

Python code

Uploaded by

Nidhi Chopra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
115 views

Python Programs-Part 2-Graph Theory

Python code

Uploaded by

Nidhi Chopra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Page 1 of 16

Python programs
Part – 2: Graph theory

By
Nidhi Chopra

(nidhi.chopra@gmail.com)
Page 2 of 16

Preface

Python programs in this eBook merge geometry,


coding and graph theory. Its inspiration came from the
subject of graph theory and then geometry. Python has
features and ability to make it possible & look good in
GUI.

Copyright

© Nidhi Chopra
Page 3 of 16

Index

S.No. Topic Page number


1 Introduction to Graph Theory 4
2 Introduction to Python 4
3 Basic Commands 5
4 Program 1: 9 point mystic rose 6-8
5 Program 2: 12 point mystic rose 9-11
6 Exercise 1 12-13
7 References 14
8 About author 15
9 Coming soon 16
Page 4 of 16

Introduction to Graph Theory

In mathematics, graph theory is the study of graphs,


which are mathematical structures used to model
pairwise relations between objects. A graph in this
context is made up of vertices (also called nodes or
points) which are connected by edges (also called links or
lines). Graphs are one of the principal objects of study in
discrete mathematics.

Introduction to Python

Python is a high-level, general-purpose programming


language. It supports multiple programming paradigms,
including structured (particularly procedural), object-
oriented and functional programming. It is often
described as a "batteries included" language due to its
comprehensive standard library.
Page 5 of 16

Basic Commands

S.No. Command Abbreviation Output Examples

1 FORWARD fd Moves turtle forward for fd(100)


number of times
specified

2 BACK bk Moves turtle back for bk(100)


number of times
specified

3 RIGHT rt Turns turtle right for rt(90)


number of degrees
specified

4 LEFT lt Turns turtle left for lt(90)


number of degrees
specified

5 PENUP penup Sets the turtle to move penup()


without drawing

6 PENDOWN pendown Resets to a drawing pen pendown()


when ordered to move

7 CIRCLE circle Makes a circle or radius, circle(r)


r
Page 6 of 16

Program 1: 9-point mystic rose


Page 7 of 16

Code 1: 9-point mystic rose

import turtle

v = []

t = turtle.Turtle()
s = turtle.Screen()
s.bgcolor("yellow")
t.pensize(3)
t.speed(0)
t.penup()
t.goto(-400, -400)
t.pencolor("red")
t.pendown()

for i in range(9):
v.append(t.pos())
t.forward(300)
Page 8 of 16

t.left(40) #360/9

#print(v)

# connecting

for i in range(9):
t.goto(v[0])
for j in range(9):
t.goto(v[j])
t.goto(v[i])
Page 9 of 16

Program 2: 12-point mystic rose


Page 10 of 16

Code 2: 12-point mystic rose

import turtle

v = []

t = turtle.Turtle()
s = turtle.Screen()
s.bgcolor("yellow")
t.pensize(3)
t.speed(100)
t.penup()
t.goto(-400, -400)
t.pencolor("red")
t.pendown()
for i in range(12):
v.append(t.pos())
t.forward(200)
t.left(30) #360/12
Page 11 of 16

#print(v)

# connecting

for i in range(12):
t.goto(v[0])
for j in range(12):
t.goto(v[j])
t.goto(v[i])

Note: Do you see any special pattern when the number of


vertices is even or odd? What happens when the angle of
the convex polygon (base graph) is float?
Page 12 of 16

Exercise 1: Write a python code to draw the following 8


geometric patterns (variations of the previous 2):

40-point mystic rose 30-point mystic rose

20-point mystic rose 15-point mystic rose


Page 13 of 16

25-point mystic rose 45-point mystic rose

50-point mystic rose 16-point mystic rose

Note: 1-point mystic rose will be just a dot / vertex. 2


point mystic rose will be a line. 3- and 4- point mystic rose
will be a triangle and a square with its diagonals drawn.
Page 14 of 16

References:

1. Curve Stitching: The Art of Sewing Beautiful


Mathematical Patterns by John Millington, Tarquin,
1989
2. Wikipedia
3. Various academic (python programming language)
pages and channels on YouTube
Page 15 of 16

About author

Author, Nidhi Chopra is a writer and content creator.


1. Profile: https://www.linkedin.com/in/nidhic6/
2. Scribd:
https://www.scribd.com/user/383559092/Nidhi-
Chopra
3. YouTube channel:
https://www.youtube.com/watch?v=lGAeWP5UpRA
&list=PLo_zz3TwrM7EgNyBBNDsO5xWoRSS9vtN7
4. Facebook page:
https://www.facebook.com/media/set/?set=a.28827
60398664929&type=3
Page 16 of 16

Coming soon

Python programs – part 3

You might also like