You are on page 1of 12

Graphs

What are Graphs?


• Graphs are a representation of connectivity between
elements called vertices or nodes.
• Each connection between two vertices is called an
edge.
• Graphs can be used to represent many real world
systems with connected objects and a large number
of algorithms have been developed to analyze the
graphs.
Graph Examples
• Computer Networks – Computers and Routers are
vertices and the network connections form the edges
• Country Maps – Cities form the vertices and roads
form the connections
• City Maps – intersections form the vertices and
streets between them form the edges
• Chemistry – Atoms in molecules form the vertices
and the bonds between them form the edges
• Workflow – Tasks form the vertices and task order
sequence forms the path of edges
Directed Graphs or Digraphs
• Each edge has a direction so the path only goes one direction
from one vertex to the other. Directed edges are called arcs.
• Each edge is like a one way street
• If the vertices are connected in both directions, they will have
two edges, one in each direction.
Undirected Graphs
• Undirected Graphs have two way edges, in other words all
edges are two way streets
Weighted Graphs
• In Weighted Graphs each edge has a weight which is some
value that reflects the cost or size of the edge connecting two
vertices.
• In Graphs representing locations on a map, edge weights
could be the distance between vertices, for example.
Other types of Graphs

• Cyclic Graph – Has a group of vertices connected with a cycle


of edges.
• Connected Graph – All Vertices are connected together.
• Tree Graph – A connected graph without any cycles, any node
could be made the root of a tree
Representing Graph Data Structures
• Graphs are expressed mathematically as G = (V, E) where V is
a set of Vertex objects and E is a set of Edge Objects.
• Each Edge Object connects two of the Vertices.
• The Edge Objects can have a direction (for directed graphs)
and/or have a weight property (for weighted graphs)
• There are two common ways of representing graphs
– As a matrix of connections called an adjacency matrix
– As a list of connections for each vertex, called adjacency lists
Adjacency Matrix Representation
• An adjacency matrix is a means of representing which vertices
(or nodes) of a graph are adjacent to which other vertices.
Adjacency List Representation
• Each Vertex has a linked list of pointers to all Vertices
connected to it by an edge
• Undirected edges (two way) will have both Vertices in each
other’s lists
• Directed edges will only have the destination vertex in the list
for the source vertex.
• Adjacency lists are especially good for large sparsely
connected graphs
• Adjacency matrices are especially good for highly connected
graphs
Adjacency List Representation

The graph pictured above has this adjacency list representation:

a adjacent to b,c
b adjacent to a,c
c adjacent to a,b
“Strive not to be a success, but rather to be of
value.”
–Albert Einstein

You might also like