Identity for sum of squares of vertex degrees in graph I am trying to understand this identity but I don't see how they count the same thing:
Given a graph $G$
\begin{align*}
\sum_{x \in V(G)} d(x)^2 = \sum_{xy \in E(G)} d(x) + d(y)
\end{align*}
I am looking for a good explanation of why this identity is true.
 A: Consider a specific vertex $x\in V(G)$ and see how much it contributes to the sum of each side of the equation.
On the LHS, it is clear to see that it contributes $d(x)^2$ to the overall total.
On the RHS however, it is less apparent.  However, we can see that $x$ will be present in exactly $d(x)$ different edges.  For each of those edges, the amount of $d(x)$ will be added (in addition to the degree of the other end, however those can be grouped up when considering that vertex's contribution)
As such, there will be $d(x)$ occurrences of adding $d(x)$ to the total sum for each specific $x$.  In other words, each $x$ contributes $d(x)\cdot d(x)=d(x)^2$ to the total sum.
Hence, the sums are the same.
A: (I recently found this older question, and found that the earlier answers were not easy to wrap my head around, and they did not add much understanding for me.   So here is my take.)
The key part of the OP's question is: what are the left and right hand sides of this equation counting?
And the answer to that is: the number of 'walks' of length 2.
(This is probably not official terminology, but by 'walk' I mean a path that is directed, so $\;A \to B \to C \not= C \to B \to A\;$, allowing duplicates, e.g. $\;A \to B \to A\;$.)

Now, before trying to give a more 'calculational' proof of this equality, $%
 \require{begingroup}
 \begingroup
 \newcommand{\calc}{\begin{align} \quad &}
\newcommand{\op}[1]{\\ #1 \quad & \quad \unicode{x201c}}
\newcommand{\hints}[1]{\mbox{#1} \\ \quad & \quad \phantom{\unicode{x201c}} }
\newcommand{\hint}[1]{\mbox{#1} \unicode{x201d} \\ \quad & }
\newcommand{\endcalc}{\end{align}}
\newcommand{\Ref}[1]{\text{(#1)}}
\newcommand{\then}{\Rightarrow}
\newcommand{\when}{\Leftarrow}
\newcommand{\edge}[2]{\{#1,#2\} \in E(G)}
%$ note that the expression $$\tag{*}\sum_{\edge x y} d(x) + d(y)$$ is a potentially confusing, for two separate reasons.
First, this summation runs over all edges (and not over all ordered pairs of vertices, as $$\sum_{\substack{x,y \in V(G) \\ \edge x y}}$$ does, which visits each edge twice).  So $\Ref{*}$ really says, "sum over all $\;p \in E(G)\;$, where the unordered pair $\;p\;$ is split into its two parts $\;x\;$ and $\;y\;$, in arbitrary order.
Second, summing over unordered pairs $\;\{x,y\}\;$ only works because the expression $\;d(x)+d(y)\;$ is symmetrical in $\;x\;$ and $\;y\;$: to write for example $$\sum_{\edge x y} d(x) + 2 d(y)$$ would be meaningless.
For these reasons, I prefer to write $\Ref{*}$ as $$\tag{*'}\sum_{\substack{x,y \in V(G) \\ \edge x y}} d(x)$$ which is the same but now the summation runs over ordered vertex pairs, not edges.

With the above, the OP's equality then becomes
$$
\renewcommand{\edge}[2]{\{#1,#2\} \in E}
\tag{0}
\sum_x d(x)^2 = \sum_{\substack{x,y \\ \edge x y}} d(x)
$$  (Note how I leave out $\;\in V(G)\;$ throughout, and write $\;E\;$ instead of $\;E(G)\;$, to keep things readable.)  And this puts us in a position to prove $\Ref{0}$, using the definition of 'degree':
$$
\tag{1}
d(x) = \sum_{\substack{y \\ \edge x y}} 1
$$
The proof is just a matter of expanding each side of $\Ref{0}$ using definition $\Ref{1}$, and then combining all summations.  For the left hand side we have
$$\calc
    \tag{LHS}
    \sum_x d(x)^2
\op=\hints{expand square; expand definition $\Ref{1}$ of $\;d(x)\;$, twice,}\hint{using different variables to prevent confusion later}
    \sum_x \left(\sum_{\substack{y \\ \edge x y}} 1\right) \cdot \left(\sum_{\substack{z \\ \edge x z}} 1\right)
\op=\hint{move $\;\cdot \sum_{\substack{z \\ \edge x z}} 1\;$ into second sum}
    \sum_x \sum_{\substack{y \\ \edge x y}} \left(1 \cdot \sum_{\substack{z \\ \edge x z}} 1\right)
\op=\hint{simplify; merge nested summations}
    \sum_{\substack{x,y,z \\ \edge x y \\ \edge x z}} 1
\endcalc$$
And for the right hand side we simply have
$$\calc
    \tag{RHS}
    \sum_{\substack{x,y \\ \edge x y}} d(x)
\op=\hint{expand definition $\Ref{1}$ of $\;d(x)\;$}
    \sum_{\substack{x,y \\ \edge x y}} \sum_{\substack{z \\ \edge x z}} 1
\op=\hint{merge nested summations}
    \sum_{\substack{x,y,z \\ \edge x y \\ \edge x z}} 1
\endcalc$$
This concludes the proof that both sides of $\Ref{0}$ are equal.

Finally, note how $\;\sum_{\substack{x,y,z \\ \edge x y \\ \edge x z}} 1\;$ indeed directly expresses that we are counting 'walks' of length 2, and the left and right hand sides of $\Ref{0}$ are counting them in two different ways.$%
\endgroup
%$
A: For a given vertex $x$, the number of times $d(x)$ appears in the sum on the right-hand side is equal to the number of edges that contain $x$, i.e. $d(x)$.
