Graphs
Graphs Graphs is a data structure that differ from all the others in one major concept , each node may have multiple predecessors as well as multiple successors . Graphs are very useful structure . They can be used to solve complex routing problems ,such as : * designing and routing airlines among the air ports they serve . * route messages over a computer network from one node to another .
Terminology Graph collection of nodes (vertices) collection of line segments connecting vertices (edges) Graph may be : Directed Each line segment has a direction (Arc) to its successor Undirected No direction on any line segment (Edge)
Neighbors Two vertices are said to be neighbors if an edge directly connects them Path A sequence of vertices in which each vertex is adjacent to the other Cycle A path that start and ends with the same vertex Loop A single Arc that begins and ends with the same vertex
Strongly connected If there is a path from each vertex to every other vertex in the directed graph . Weakly connected If at least two vertices are not connected Disjoint A graph is disjoint if it not connected
Weakly connected A B E G F C D H I
Strongly connected A B E G F C D H I
Disjoint A B E G F C D H I
Degree of vertex Is the number of lines incident to it . Out degree of vertex in digraph Is the number of arcs leaving the vertex Indegree Is the number of arcs entering the vertex
Operations Add Vertex Insert a new vertex in the graph When a vertex is added it is disjoint , it is not connected to any other vertices in the list . B A B A AddVertix C C E E
Delete Vertex Removes a vertex from the graph .When a vertex is deleted , all connecting edges are removed . B A B A C E C E
Connect a vertex to a destination vertex . Add edge Connect a vertex to a destination vertex . If the graph is a digraph , then one of the edges must be the source and the other is the destination . B A B A C E C E
Find Vertex Traverse the graph looking for a specified vertex. If the vertex is found , its data are returned . If it is not founded , an error is indicated .
Graph Storage Structures To represent a graph , we need to store two sets : The vertices of the graph The edges or arcs . The two common structures used to store these sets are array and linked list .
Adjacency matrix Uses a vector ( one –dimensional array ) for the vertices and a matrix (two - dimensional array) to store the edges .
Adjacency list Uses a two- dimensional ragged array to store the edges . The vertex list is a singly linked list of the vertices in the list .