Longest carbon chain
Welcome back to CodeReview. Your code and your problem seemed familiar, so I searched and found your previous attempt. This version runs to completion on my machine, so congratulations!

What is this?

First, I have to ask, what are you doing? The code runs to completion, and prints an answer that looks like what you're trying to get, but that answer is lost in the noise of a bunch of other stuff. This is nuts! When you're asking for review, you want to make sure everything is clear. Please, make your output more clear!

$ python carbon.py
[0, 0, 0, 0, 0]
[1, 9, 1, 9, 1]
[0, 0, 9, 0, 0]
[0, 0, 1, 0, 0]
[0, 0, 9, 0, 0]
[0, 0, 1, 0, 0]
[0, 0, 0, 0, 0]
[(1, 0), (1, 2), (1, 4), (3, 2), (5, 2)]
[(1, 1), (1, 3), (2, 2), (4, 2)]
[1, 1, 2, 4]
[1, 3, 2, 2]
True
0
False
True
False
False
False
1
True
False
True
True
False
2
False
True
False
False
False
3
False
True
False
False
True
4
False
False
False
True
False
8
[((1, 0), [(1, 2)]), ((1, 2), [(1, 0), (1, 4), (3, 2)]), ((1, 4), [(1, 2)]), ((3, 2), [(1, 2), (5, 2)]), ((5, 2), [(3, 2)])]
{'(1, 0)': ['(1, 2)'], '(1, 2)': ['(1, 0)', '(1, 4)', '(3, 2)'], '(1, 4)': ['(1, 2)'], '(3, 2)': ['(1, 2)', '(5, 2)'], '(5, 2)': ['(3, 2)']}
['(1, 0)', '(1, 4)', '(5, 2)']
['(1, 0)']
['(1, 0)', '(1, 2)', '(1, 4)']
['(1, 0)', '(1, 2)', '(3, 2)', '(5, 2)']
['(1, 4)', '(1, 2)', '(1, 0)']
['(1, 4)']
['(1, 4)', '(1, 2)', '(3, 2)', '(5, 2)']
['(5, 2)', '(3, 2)', '(1, 2)', '(1, 0)']
['(5, 2)', '(3, 2)', '(1, 2)', '(1, 4)']
['(5, 2)']
4
But


Why use numpy?

Next question: why are you using numpy? Here's the top of your code:

import numpy as np
inputgrid = np.array([['.','.','.','.','.'],
                      ['C','-','C','-','C'],
                      ['.','.','|','.','.'],
                      ['.','.','C','.','.'],
                      ['.','.','|','.','.'],
                      ['.','.','C','.','.'],
                      ['.','.','.','.','.']])# This Is Input


inputgrid = np.where(inputgrid=='.', 0, inputgrid)
inputgrid = np.where(inputgrid=='C', 1, inputgrid)
inputgrid = np.where(inputgrid=='-', 9, inputgrid)
inputgrid = np.where(inputgrid=='|', 9, inputgrid)

np.array(inputgrid).tolist()

grid = [[int(item) for item in row] for row in inputgrid]


You import numpy, create a numpy 2d array, process it minimally, and then convert the numpy array into a standard Python list of lists.

My first suggestion is this: stop using numpy. You aren't doing anything (yet) that justifies numpy. (Numpy isn't bad. But numpy is for numbers, and you're doing graphs. Maybe use a graph package instead?)

Style

I have two major objections to your code as presented. First, much of your code is "up against the wall" -- you are writing code that is executed immediately, rather than put it into functions that allow you to call or not-call your code as you like, and which furthermore logically group related parts of your code together. Please organize your code into descriptive functions!

Second, towards the end you have functions shortestpath and Prfix. Those are both PEP-8 violations. They should be shortest_path and prefix respectively. Every time you ask a question on here, you're going to get dinged for not following PEP8, so ... CONFORM!!!

Organization

As I see it, your code does a few things in sequence:


Converts a text representation of a molecule into a data structure of some kind.
Builds an adjacency graph
Computes the length of the longest carbon chain
Prints the length of the longest chain
Prints a prefix?


Let's see if we can improve on those.

Input

You represent your input as an ndarray, carefully and meticulously (&amp; painfully) entered, with each element requiring 4 keystrokes: 'C', (apostrophe, C, apostrophe, comma). That's not going to scale very well, and it's going to be very prone to errors. 

I suggest you use Python's triple-quoted strings to create a single large block of text, and then parse that as a text-processing problem in order to get your data:

Molecule = """

     C-C-C
       |
       C
       |
       C

""".strip()


You should strip all the starting and stopping whitespace, since that contributes nothing. You should remove tabs, replacing them with spaces in a rational way. The str.expandtabs() method will do that for you. You can then look at textwrap.dedent to evenly remove leading whitespace. This will give you a "minimal" ASCII-picture to parse for your graph.

If you have learned classes, I suggest you create an Atom class for your initial data structure. You can use it to build up the graph:

class Atom:
    def __init__(self, element='C', bonds=[]):
        self.element = element
        self.bonds = bonds


I don't know if this is meaningful in your case, but some graphs I have seen use multiple lines to indicate bonds, like:

C=C-C


Depending on what you're using this for, you may or may not care about that. You could just store that as a tuple in the bonds list, or create a duplicate bond entry, or a separate "weights" list to maintain in parallel, or create a Bond class that would represent the link and the number, etc.

Path length

There are a lot of ways to do path length. If you want to support large molecules or arbitrary diagrams, you might want to look at a package graph library. For example, what should your code return given this diagram?

C-C    C-C-C


Is that two molecules? Should it return the longest (3) or a list of the various longest-paths ([2, 3]) or what? It's up to you. I'd suggest that this is an error in input. But if you try to support this, it will change what graphing approach you use, since connectivity is a major concern in most graph traversal algorithms.

If you elect do compute path length yourself, you will likely find that a simple recursive traversal works for most simple cases (depth-first search). Just compute the max length of each outward bond, then sum the two highest.

Bug

Beware of this bug in your current code:

C-C
| |
C C


Your adjacentnode function doesn't check the intervening space, so it considers the bottom two carbons to be "adjacent" (which is true) but you're using that for your adjacency list computation (which would be wrong).