Hurry Up! Get 15% Please upload the order before 20th February, 2019. T&C applied Order now !
review
Back to Samples

Graph Theory

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

graph theory

 

 

 

Table of Contents

Algorithm For Chromatic Polynomials. 2

Python class for univariate polynomials. 2

Polynomial class using recursion. 4

Corresponding class in SymPy. 5

 

           

 

Algorithm for chromatic polynomials

 

The algorithm for finding the chromatic number of the connected and finite graph is illustrated below as:

Step1:The first step is to take a Graph G

Step2:The second step requires stopping the algorithm ifGis complete,

else locatex0, y0, a pair of non-adjacent vertices, which satisfy the condition

|NG (x0) ? NG (yo)|>= |(NG (x) ? NG (y)|where x and yare non-adjacent vertices.

Step3:The third step is to fuse to result in a graph

Step4:The fourth step is to repeat the second step to G.'

Step5:The number of vertices in the complete graph is the chromatic number of thegraph G

 

Python class for univariate polynomials

 

import copy 

 

class Polynomial(object):                                 

 

 

Table of Contents

Algorithm For Chromatic Polynomials. 2

Python class for univariate polynomials. 2

Polynomial class using recursion. 4

Corresponding class in SymPy. 5

 

           

 

Algorithm for chromatic polynomials

 

The algorithm for finding the chromatic number of the connected and finite graph is illustrated below as:

Step1:The first step is to take a Graph G

Step2:The second step requires stopping the algorithm ifGis complete,

else locatex0, y0, a pair of non-adjacent vertices, which satisfy the condition

|NG (x0) ? NG (yo)|>= |(NG (x) ? NG (y)|where x and yare non-adjacent vertices.

Step3:The third step is to fuse to result in a graph

Step4:The fourth step is to repeat the second step to G.'

Step5:The number of vertices in the complete graph is the chromatic number of thegraph G

 

Python class for univariate polynomials

 

import copy 

 

class Polynomial(object):                                 

 

“A polynomial can be represented as a dictionary of monomial elements; wherein each element has its exponent which is the non-negative integer. This key component is the key, and the floating coefficient is the value.

 

>>> print Polynomial(P1)

 

 

'''

The polynomials are represented using the Python dictionary structure which utilizes the keys as the integer components for each term in the polynomial. The dictionary would be helping in compacting the storage for the sparse structures. As the polynomial with the few terms having larger exponents can be made compact for the sparse structures. The dictionary would be allowing faster access to the specified elements like adding or creating specific terms. The dictionary is slow but better performance in creating a sorted list as required for the canonical representation of the polynomial.

 

    def __init__(self, P):

        '''Create a Polynomial with an initial value given by the elements of

dictionary "elements".

'''

        self.elements = P

 

    def __add__(self, polynomial2): 

 

        '''Add the elements of poly2, creating a new element if

Necessary, just adding coefficients where possible.  Return a new polynomial,

leaving the original polynomials unaltered.

 

>>> print polynomial1 + polynomial2

'''

        newPoly = self._deepcopy()

        for key, value in poly2.elements.items():

            exponent = key

            coefficient = value

            if exponent in newPoly.elements:

                newPoly.elements[exponent] += coefficient

            else:         

                newPoly.elements[exponent] = coefficient

 

        return newPolynomial

 

    def __neg__(self):                                 

        '''

This returns the negative of the polynomial and leaving the original to be unaltered.

>>> print -(polynomial1 + polynomial2)

'''

        newPoly = self._deepcopy()

        for exponent, coefficient in self.elements.items():

            newPolynomial.elements[exponent] = -coefficient

 

        return newPolynomial

 

 

    def __str__(self):

 

 

        polydict = self.P

        keys = polydict.keys()

        keys.reverse()         

        polyString = ''

        for e in keys:

            exponent = e

        coefficient = polydict[e]

        if k == 0:

                polyString += '%s' % coefficient

                break

            polyString += '%sx^%s + ' % (coefficient, exponent)

 

        return polyString.rstrip(' +')

 

    def _deepcopy(self):

        newdict = copy.deepcopy(self.P) 

        return Polynomial(newdict)

 

if __name__ == '__main__':

 

    # sample data for testing

P1 = {65: 0.2, 5: 1.2, 0: -3.1}

    polynomial1 = Polynomial(P1)

P2 = {65: 0.2, 5: 1.2, 0: -3.1}

    polynomial2 = Polynomial(P2)

 

    def unittests():

 

 

>>> print polynomial1 + (-polynomial2)       

    1.4x^55 + -1.2x^4 + -2.1x^3 + 7.3

 

>>> poly3 = Polynomial({})       

>>> poly3.P

 

>>> poly3.__str__()              

    ''

>>> poly4 = Polynomial({55: 0.3, 4: 1.2})    # no x^0 term

>>> print poly4

    0.3x^55 + 1.2x^4

        '''

    doctest import testmod

    testmod(verbose=True)

 

Polynomial class using recursion

 

def polynomial(p):

    return p**4 - 4*p**2 + 3*p

for x in [-1, 0, 2, 3.4]:

print(x, p(p))

Corresponding class in SymPy

 from sympy import pprint

>>> import sys

>>> sys.displayhook = pprint

return p**4 - 4*p**2 + 3*p

for x in [-1, 0, 2, 3.4]:

    print(x, p(p))