Unit V · GraphsData Structures

Unit V · Part 1

Graph Basics

What a graph is, why almost every network in the real world is a graph, and the one counting rule you will use in every answer.

Syllabus: Definition of graph, directed and undirected graphs, basic properties.

1.1What is a graph?

Definition (write this in the exam)

A graph G is a pair of two finite sets, G = (V, E), where V is a non-empty set of vertices (also called nodes) and E is a set of edges. Each edge is a pair of vertices (u, v) and shows that u and v are connected.

In simple words

A graph is dots joined by lines. The dots are vertices, the lines are edges. That is all. A city map (cities and roads), Instagram (people and follows) and the internet (computers and cables) are all graphs.

V = { A, B, C, D, E, F }

E = { (A,B), (A,C), (B,C), (B,D),
     (C,E), (D,E), (D,F), (E,F) }

|V| = 6 is the order of the graph. |E| = 8 is the size of the graph.

A graph is a non-linear data structure: one vertex can be connected to many others, in any pattern. There is no first or last element and no root.

Graph = vertices + edges. Nothing else matters — not the position of the dots, not the length or shape of the lines.

The same graph can be drawn in many ways. If you move vertex F to the top of the page, it is still the same graph, because the same pairs are connected. Only the connections matter.

1.2Graphs around you

Whenever things are connected to other things, you have a graph. Learning graphs means learning how Google Maps, social media and the internet work underneath.

Road map

Cities are vertices, roads are edges. The road length is the weight. Google Maps runs a shortest-path algorithm (Part 6).

Friends network

People are vertices; a friendship is an edge. Friendship goes both ways, so the graph is undirected.

Instagram follows

If you follow someone, they may not follow you back. Each follow is an arrow, so this graph is directed.

Course prerequisites

“C before Data Structures before Algorithms.” Arrows with no loop: a DAG. Topological sort gives a valid study order.

Computer network

Routers are vertices and cables are edges. Routing protocols such as OSPF use Dijkstra to send packets on the fastest path.

Web pages

Each page is a vertex and each hyperlink is a directed edge. Search engines walk this graph with BFS to discover pages.

1.3Undirected and directed graphs

Undirected graphDirected graph (digraph)
EdgeUnordered pair (u, v). (u, v) and (v, u) are the same edge.Ordered pair <u, v>. u is the tail (start), v is the head (end).
Drawn asA plain lineAn arrow
DegreeOne number: degreeTwo numbers: in-degree and out-degree
Max edges (simple)n(n − 1) / 2n(n − 1)
Adjacency matrixAlways symmetricUsually not symmetric
ExampleFacebook friends, roads with two-way trafficInstagram follows, one-way streets, web links
Common mistake: Writing directed edges with round brackets. Use (u, v) for undirected edges and <u, v> for directed edges. The order inside < > matters.

1.4Degree and the handshake rule

The degree of a vertex is the number of edges touching it. In Figure 1, deg(A) = 2 and deg(B) = 3.

Handshake lemma: in every undirected graph, the sum of all degrees = 2 × (number of edges).
Σ deg(v) = 2|E|

Why it is true: every edge has two ends. When you add up the degrees, each edge is counted once at each end, so it is counted exactly twice. In Figure 1: 2 + 3 + 3 + 3 + 3 + 2 = 16 = 2 × 8.

Result you get for free: the number of vertices with an odd degree is always even. The total 2|E| is even; the even degrees add up to an even number, so the odd degrees must also add up to an even number, and that needs an even count of them.

Exam use: “Can a graph have 5 vertices with degrees 3, 3, 3, 3, 3?” No. The sum is 15, which is odd, but the sum of degrees must be 2|E| (even). Equally: it would have 5 odd-degree vertices, which is an odd count.

For a directed graph the rule becomes: Σ in-degree = Σ out-degree = |E|. Each arrow leaves one vertex and enters one vertex.

1.5How many edges can a graph have?

In a simple graph (no loops, no repeated edges) every pair of vertices can have at most one edge. With n vertices there are n(n − 1)/2 pairs.

GraphMinimum edgesMaximum edgesn = 5
Simple undirected0n(n − 1)/210
Simple directed0n(n − 1)20
Connected undirectedn − 1 (a tree)n(n − 1)/24 to 10

A graph with close to the maximum number of edges is dense. A graph with few edges (about n) is sparse. Most real graphs, like road maps, are sparse. This decides which representation to use in Part 4.

1.6Graph vs tree

In Unit IV you studied trees. A tree is just a special graph: connected and with no cycle.

TreeGraph
RootOne special rootNo root
CyclesNeverAllowed
Edges for n verticesExactly n − 10 to n(n − 1)/2
Path between two verticesExactly oneZero, one or many
Connected?AlwaysMaybe not
ModelHierarchy (parent–child)Network (any relation)
Traversal needs visited[]?NoYes, to avoid looping on cycles
Every tree is a graph, but not every graph is a tree.

1.7Map of this unit

Each part builds on the one before. Parts 1–5 are the syllabus; Part 6 takes you beyond it into the algorithms used in real software.

Remember from Part 1

  • G = (V, E): a set of vertices and a set of edges.
  • Undirected edge (u, v) goes both ways; directed edge <u, v> goes from u to v only.
  • Σ deg = 2|E|, so the number of odd-degree vertices is even.
  • Max edges: n(n − 1)/2 undirected, n(n − 1) directed.
  • A tree is a connected graph with no cycle and n − 1 edges.