Graphs are powerful data structures that model relationships between entities. They consist of vertices (nodes) and edges, which can be directed or undirected, weighted or unweighted. This flexibility allows graphs to represent various real-world scenarios, from social networks to road systems.
Vertices in graphs have properties like degree, which measures their connectivity. Different graph types, such as complete, bipartite, and connected graphs, have unique characteristics that make them suitable for specific applications. Understanding these fundamentals is crucial for solving complex problems using graph algorithms.
Graph Fundamentals
Vertices and edges in graphs
- Graph is a data structure with vertices (nodes) and edges connecting them
- Vertices represent entities or objects
- Edges represent relationships or connections between vertices
- Can be weighted with numerical value for cost or strength of connection
- Can be unweighted with no associated value, just represents connection
- Can be directed with specific direction from one vertex to another (arrow)
- Can be undirected with no specific direction, bidirectional connection
Directed vs undirected graphs
- Directed graphs (digraphs) have edges with specific direction (arrows)
- Social media follower relationships (edge from user A to B indicates A follows B)
- Web page links (edge from page A to B indicates hyperlink from A to B)
- Task dependencies (edge from task A to B indicates A must be completed before B)
- Undirected graphs have edges without specific direction, bidirectional connections
- Social networks representing friendships (edge between two people indicates mutual friendship)
- Road networks (edge between two cities represents connecting road)
- Collaboration networks (edge between two individuals indicates they have worked together on a project)
Vertex Properties
Degree concepts for vertices
- Degree of vertex in undirected graph is number of edges incident to that vertex (number of edges connected to vertex)
- In directed graph, vertices have two types of degrees:
- In-degree: Number of incoming edges to a vertex (edges that point towards vertex)
- Out-degree: Number of outgoing edges from a vertex (edges that point away from vertex)
- Total degree of vertex in directed graph is sum of its in-degree and out-degree
Classification of graph types
- Complete graphs:
- Every pair of vertices is connected by an edge
- In complete graph with $n$ vertices, there are $\frac{n(n-1)}{2}$ edges
- Bipartite graphs:
- Vertices can be divided into two disjoint sets
- Every edge connects a vertex from one set to a vertex in the other set
- No edges between vertices within the same set
- Job assignment problems (one set represents workers, other set represents tasks)
- Student-course enrollment (one set represents students, other set represents courses)
- Connected graphs:
- There exists a path between every pair of vertices
- Starting from any vertex, possible to reach any other vertex by traversing edges
- Graph that is not connected is called disconnected graph, consists of two or more connected components