0% found this document useful (0 votes)
6 views8 pages

Maths 12th

The document outlines an assignment for students at K.K. Wagh Arts, Commerce, Science & Computer Science College, focusing on the use of the NetworkX library in Python to create and analyze various types of trees, including balanced binary trees and ternary trees. Tasks include drawing trees of specified heights, verifying relationships between vertices and edges, and calculating tree properties such as center, radius, and diameter. The document includes code snippets for implementing these tasks using Python and NetworkX.

Uploaded by

Sahil Tarle
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
6 views8 pages

Maths 12th

The document outlines an assignment for students at K.K. Wagh Arts, Commerce, Science & Computer Science College, focusing on the use of the NetworkX library in Python to create and analyze various types of trees, including balanced binary trees and ternary trees. Tasks include drawing trees of specified heights, verifying relationships between vertices and edges, and calculating tree properties such as center, radius, and diameter. The document includes code snippets for implementing these tasks using Python and NetworkX.

Uploaded by

Sahil Tarle
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 8
al Karmaveer Kakasaheb wagh education society's K.K. WAGH ARTS, COMMERCE, SCIENCE & COMPUTER SCIENCE COLLEGE CHANDORI, Tal. Niphad,Dist.Nashike422 209 Sub:_prathematics PsuckicalIl class: EYBCS sateh:_Ao. Expt. No:, a Dates, Roll No;__36 Name:_Ghubhern Scmpat Jadhav Remarks Assignment 12 + Using networkx Feorn python do the Following 1. Draw balanced binary trees of height 254 and etc.| 2 Draw terery trees of height | and 3 eke- S. Draw any novay tree of height h Rr given n and h. 4 Find the no. of vertices and edges in given krees » verify the elation between them. 5. Find center radius and diomeler of above trees. ________] save water & Electricity @ scanned with OKEN Scanner ue irnport netwoskx_as_hx. as ple Up| import _matplotlib.pyplot lef create _ balanced sbinary.ttree (height) 2 fe nx-GraphC) node_ Counter) + def add-edges (parent current. height if Current. height = = height: return _node- counter left = node — Counter Hight = node. counter +] T.add ~edge (parent flere) T- add edge (parent, right) | hode counter = add_ edges Clery current _ height +1 node counter + 2) Node _ countex=add_. edges (righb Current. height + | nede_ counter) beturn ae counter add_edges(o, |!) return T ue def draw balanced — binary — tree(height ): tree = create _ balanced — free (height) ple. Figure (figsive = (8,6)) pos =nx. Spring. layout (free _, seed =y2) nidraw (bree P08 with. labels= True node_size= SO, nede.coloy = “lightllye”, edqe_colo® x.“a-yay” i font Size =10) at PItE-Showe ) @ scanned with OKEN Scanner «| Ple-ghowt 7 Lap | } ‘ Jajap>.di 1 1p draw -balancedbinawy tree 3). ppd i ofp P oe i i foo>- 2h 6 2It is. balanced binavy +ree of height 2- @ scanned with OKEN Scanner age Xo] [War J i TP import netwovkx as_hx ~-G2 IZJP import matplotlib-pypiot_as_plé Lp def create tevnarytree (height): T=nx.Graph() def add_edges (parent ,current-height , node — counter) : | iF_current_ height == height : | return node_counte Children= Code counter +i for {in range (3)], ieee for child in children ais T.add_edge (parent ,child ) Node _countex= add = edges (Children Co] current height +1 hode-counter +3 ) node counte+= add zedges (childrenfit.y current aheight + i hhode . Counter } “ node ~ counter = add_ edge: (childrenC2], current height +1 | node. countetr) return _node—countex add_edges(o 11) i 5 retumn To Jp _deF drouwsaternamy free (height): qT J oT tree = create = =temarny- tree Cheight) ey Nnx.drow Chree | wilh = label's =pue) ple: Showc > i TIP draw ternary tree (3) @ scanned with OKEN Scanner [om be! ooo. sbolk! igisternary tree of height 2. JU ; eet TP impost- networkx as~nx Tjelimpoxt — matplotlib.pyplot as pit oe Ie der Creake nary tvee CMsh): Tenx-Graphe) \ Wod def add. edges Cp ch ne): if ch=sh: retum he : Lbs c=Ene+i fox iin rangecn)] vi D for child inc: T.add.edge Cp . child ) ne add. edges (¢ Co che biyne+n.) | Pox child in efile d.s ne= add. edges (child ,ch+!_yne) retusn Ac add. edges Cott) tetum i= @ scanned with OKEN Scanner Typ ldef draw -n-ary. tree Cnyh)? tree =create navy _ tree Chyh) x. d-vow (tree , with — labels = True), i Plt Show? ) aval ot draw nary -tree (3,2) s& axy tree of height il rye: import networks —'as-nx +. | 4 bor T[fideF create tree (n): _Tenx:pathagraph(n) |! retuen T wl Tip def verify tree retation(T): N=T. number. of -nodesc_) m= T. numberof edges c.). if m==n-1: -prink(p' in tree number Of vertices ‘in fn} and edges are {my}, it is ‘verified the relation man-I") @ scanned with OKEN Scanner ae + 1 i = ye | T= create tree (6) ‘ i Qowrk ai — Te | verify tress — relation (T) . cand ofp. in tree number of vertices in 6 and edges are s,it is verified the relation _m=n-t TIP import _networkx as _nx Lp_def create balance: | — binary ~ tree heig ht) + sel T=nx-Graph¢) def _add-edges (parent. current height , node — counter) * if current — height = =height! return node ~counte+ left = node~ counter tight = node _ counters | T.add—edge parent leFE) Tadd. dge Cpare nt. right) node counter sadd_ edges CleFe: curment sheight'+l node. bor 224s pleunkyy 2} node counter = add. edges Crightscurrent:. height +1 nede-_counter) | return node... counber reyhot \ add -edges Costs!) return T qe Tl =creake balanced — binary albwee(3.) | Cc enx.centex CTL) rye print 'centes ce) oe. icentex : [0] @ scanned with OKEN Scanner Tp_renx. radius (t1) TP. guint (radius ",r) ofe radivs i 2 tye _d=nx.diameter (7!) Lip evint('diametes :’ oye diameter : 4 d) 3 @ scanned with OKEN Scanner

You might also like