Better names for variables [closes #18]
This commit is contained in:
parent
baee83266a
commit
11564fd4d3
4 changed files with 115 additions and 118 deletions
|
|
@ -480,7 +480,7 @@ efficiently implemented using them.
|
|||
A convenient way to store the adjacency lists is to declare
|
||||
an array of vectors as follows:
|
||||
\begin{lstlisting}
|
||||
vector<int> v[N];
|
||||
vector<int> adj[N];
|
||||
\end{lstlisting}
|
||||
|
||||
The constant $N$ is chosen so that all
|
||||
|
|
@ -503,11 +503,11 @@ For example, the graph
|
|||
\end{center}
|
||||
can be stored as follows:
|
||||
\begin{lstlisting}
|
||||
v[1].push_back(2);
|
||||
v[2].push_back(3);
|
||||
v[2].push_back(4);
|
||||
v[3].push_back(4);
|
||||
v[4].push_back(1);
|
||||
adj[1].push_back(2);
|
||||
adj[2].push_back(3);
|
||||
adj[2].push_back(4);
|
||||
adj[3].push_back(4);
|
||||
adj[4].push_back(1);
|
||||
\end{lstlisting}
|
||||
|
||||
If the graph is undirected, it can be stored in a similar way,
|
||||
|
|
@ -517,7 +517,7 @@ For a weighted graph, the structure can be extended
|
|||
as follows:
|
||||
|
||||
\begin{lstlisting}
|
||||
vector<pair<int,int>> v[N];
|
||||
vector<pair<int,int>> adj[N];
|
||||
\end{lstlisting}
|
||||
|
||||
If there is an edge from node $a$ to node $b$
|
||||
|
|
@ -541,11 +541,11 @@ For example, the graph
|
|||
\end{center}
|
||||
can be stored as follows:
|
||||
\begin{lstlisting}
|
||||
v[1].push_back({2,5});
|
||||
v[2].push_back({3,7});
|
||||
v[2].push_back({4,6});
|
||||
v[3].push_back({4,5});
|
||||
v[4].push_back({1,2});
|
||||
adj[1].push_back({2,5});
|
||||
adj[2].push_back({3,7});
|
||||
adj[2].push_back({4,6});
|
||||
adj[3].push_back({4,5});
|
||||
adj[4].push_back({1,2});
|
||||
\end{lstlisting}
|
||||
|
||||
The benefit in using adjacency lists is that
|
||||
|
|
@ -555,7 +555,7 @@ For example, the following loop goes through all nodes
|
|||
to which we can move from node $s$:
|
||||
|
||||
\begin{lstlisting}
|
||||
for (auto u : v[s]) {
|
||||
for (auto u : adj[s]) {
|
||||
// process node u
|
||||
}
|
||||
\end{lstlisting}
|
||||
|
|
@ -570,14 +570,14 @@ We can efficiently check from an adjacency matrix
|
|||
if there is an edge between two nodes.
|
||||
The matrix can be stored as an array
|
||||
\begin{lstlisting}
|
||||
int v[N][N];
|
||||
int mat[N][N];
|
||||
\end{lstlisting}
|
||||
where each value $\texttt{v}[a][b]$ indicates
|
||||
where each value $\texttt{mat}[a][b]$ indicates
|
||||
whether the graph contains an edge from
|
||||
node $a$ to node $b$.
|
||||
If the edge is included in the graph,
|
||||
then $\texttt{v}[a][b]=1$,
|
||||
and otherwise $\texttt{v}[a][b]=0$.
|
||||
then $\texttt{mat}[a][b]=1$,
|
||||
and otherwise $\texttt{mat}[a][b]=0$.
|
||||
For example, the graph
|
||||
\begin{center}
|
||||
\begin{tikzpicture}[scale=0.9]
|
||||
|
|
@ -696,7 +696,7 @@ at a given node.
|
|||
|
||||
The edge list can be stored in a vector
|
||||
\begin{lstlisting}
|
||||
vector<pair<int,int>> v;
|
||||
vector<pair<int,int>> edges;
|
||||
\end{lstlisting}
|
||||
where each pair $(a,b)$ denotes that
|
||||
there is an edge from node $a$ to node $b$.
|
||||
|
|
@ -718,18 +718,18 @@ Thus, the graph
|
|||
\end{center}
|
||||
can be represented as follows:
|
||||
\begin{lstlisting}
|
||||
v.push_back({1,2});
|
||||
v.push_back({2,3});
|
||||
v.push_back({2,4});
|
||||
v.push_back({3,4});
|
||||
v.push_back({4,1});
|
||||
edges.push_back({1,2});
|
||||
edges.push_back({2,3});
|
||||
edges.push_back({2,4});
|
||||
edges.push_back({3,4});
|
||||
edges.push_back({4,1});
|
||||
\end{lstlisting}
|
||||
|
||||
\noindent
|
||||
If the graph is weighted, the structure can
|
||||
be extended as follows:
|
||||
\begin{lstlisting}
|
||||
vector<tuple<int,int,int>> v;
|
||||
vector<tuple<int,int,int>> edges;
|
||||
\end{lstlisting}
|
||||
Each element in this list is of the
|
||||
form $(a,b,w)$, which means that there
|
||||
|
|
@ -753,10 +753,10 @@ For example, the graph
|
|||
\begin{samepage}
|
||||
can be represented as follows:
|
||||
\begin{lstlisting}
|
||||
v.push_back(make_tuple(1,2,5));
|
||||
v.push_back(make_tuple(2,3,7));
|
||||
v.push_back(make_tuple(2,4,6));
|
||||
v.push_back(make_tuple(3,4,5));
|
||||
v.push_back(make_tuple(4,1,2));
|
||||
edges.push_back({1,2,5});
|
||||
edges.push_back({2,3,7});
|
||||
edges.push_back({2,4,6});
|
||||
edges.push_back({3,4,5});
|
||||
edges.push_back({4,1,2});
|
||||
\end{lstlisting}
|
||||
\end{samepage}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue