A graph is a data structure that consists of a set of vertices (nodes) and a set of edges that connect pairs of vertices. A directed graph, also known as a digraph, is a graph in which each edge has a direction, indicating that the edge connects one vertex to another in a specific direction. An undirected graph is a graph in which each edge has no direction, indicating that the edge connects two vertices without a specific direction.
Here are examples of directed and undirected graphs:
Directed graph: A social media platform that models friendships between users as directed edges. If user A is friends with user B, there is a directed edge from A to B. This graph is directed because the relationship between users is not necessarily mutual - user A can be friends with user B without user B being friends with user A.
Undirected graph: A map that models the connections between cities as edges. If there is a road that connects city A to city B, there is an undirected edge between A and B. This graph is undirected because the connection between cities is bidirectional - if there is a road from A to B, there is also a road from B to A.
In general, directed graphs are useful for modeling relationships that have a specific direction, such as the flow of traffic on a road network or the dependencies between tasks in a project. Undirected graphs are useful for modeling relationships that are bidirectional, such as the connections between nodes in a computer network or the relationships between items in a recommendation system.