This document discusses graphs and graph algorithms. It defines a graph as a collection of vertices and lines connecting pairs of vertices. It describes directed and undirected graphs, cycles, connectedness, and graph traversal algorithms like depth-first and breadth-first search. Graph storage structures like adjacency matrices and lists are also covered. Finally, the document discusses network applications like minimum spanning trees and shortest path algorithms.
This document discusses graphs and graph algorithms. It defines a graph as a collection of vertices and lines connecting pairs of vertices. It describes directed and undirected graphs, cycles, connectedness, and graph traversal algorithms like depth-first and breadth-first search. Graph storage structures like adjacency matrices and lists are also covered. Finally, the document discusses network applications like minimum spanning trees and shortest path algorithms.
Copyright:
Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online from Scribd
This document discusses graphs and graph algorithms. It defines a graph as a collection of vertices and lines connecting pairs of vertices. It describes directed and undirected graphs, cycles, connectedness, and graph traversal algorithms like depth-first and breadth-first search. Graph storage structures like adjacency matrices and lists are also covered. Finally, the document discusses network applications like minimum spanning trees and shortest path algorithms.
Copyright:
Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online from Scribd
A graph is a collection of nodes, called vertices, and a
collection of segments, called lines, connecting pairs of vertices. In other words, a graph consists of two sets, a set of vertices and a set of lines.
Topics discussed in this section:
Basic Concepts •Directed and Undirected Graphs •Cycles and Loops •Connected and Disjoint Graphs Graph Traversal Graph Storage Structures Computer Science: A Structured Programming Approach Using C 1 FIGURE 15-45 Directed and Undirected Graphs
Computer Science: A Structured Programming Approach Using C 2
Note Graphs may be directed or undirected. In a directed graph, each line, called an arc, has a direction indicating how it may be traversed. In an undirected graph, the line is known as an edge, and it may be traversed in either direction.
Computer Science: A Structured Programming Approach Using C 3
Note A file is an external collection of related data treated as a unit.
Computer Science: A Structured Programming Approach Using C 4
FIGURE 15-46 Cycles and Loops
Computer Science: A Structured Programming Approach Using C 5
FIGURE 15-47 Connected and Disjoint Graphs
Computer Science: A Structured Programming Approach Using C 6
Graph Traversal To investigate all the vertices in a graphs in some systematic order for problem solving. In graphs, we often do not have any one vertex singled out as special, and therefore the traversal may start at an arbitrary vertex. Two graph traversal methods: Depth-first traversal Breadth-first traversal
Computer Science: A Structured Programming Approach Using C 7
Depth-first traversal Roughly analogous to preorder traversal of an ordered tree. Suppose that the traversal has just visited a vertex v, and let w1, w2, …, wk be the vertices adjacent to v. Then we shall next visit w1 and keep w2, ..., wk waiting. After visiting w1, we traverse all the vertices to which it is adjacent before returning to traverse w2, …, wk. Depth-first traversal is naturally recursive (or can use a stack).
Computer Science: A Structured Programming Approach Using C 8
FIGURE 15-48 Depth-first Traversal of a Tree
Computer Science: A Structured Programming Approach Using C 9
Note In the depth-first traversal, all of a node’s descendents are processed before moving to an adjacent node.
Computer Science: A Structured Programming Approach Using C 10
FIGURE 15-49 Depth-first Traversal of a Graph
Computer Science: A Structured Programming Approach Using C 11
Breadth-first traversal Roughly analogous to level-by-level traversal of an ordered tree. If the traversal has just visited a vertex v, then it next visits all the vertices adjacent to v, putting the vertices adjacent to these in a waiting list to be traversed after all vertices adjacent to v have been visited. Breadth-first traversal normally uses queue.
Computer Science: A Structured Programming Approach Using C 12
FIGURE 15-50 Breadth-first Traversal of a Tree
Computer Science: A Structured Programming Approach Using C 13
FIGURE 15-51 Breadth-first Traversal of a Graph
Computer Science: A Structured Programming Approach Using C 14
Note In the breadth-first traversal, all adjacent vertices are processed before processing the descendents of a vertex.
Computer Science: A Structured Programming Approach Using C 15
Graph Storage Structures
To represent a graph, we need to store two sets. The first set
represents the vertices of the graph, and the second set represents the edges or arcs. The two most common structures used to store these sets are arrays and linked lists.
• Adjacency Matrix • Adjacency List
Computer Science: A Structured Programming Approach Using C 16
Computer Science: A Structured Programming Approach Using C 17 Computer Science: A Structured Programming Approach Using C 18 Application: Networks
A network is a graph whose lines are weighted. It is also known
as a weighted graph. Included in this section are two graph applications that process networks.
• Minimum Spanning Tree
• Shortest Path Algorithm
Computer Science: A Structured Programming Approach Using C 19
Computer Science: A Structured Programming Approach Using C 20 Computer Science: A Structured Programming Approach Using C 21 Computer Science: A Structured Programming Approach Using C 22 Computer Science: A Structured Programming Approach Using C 23 Computer Science: A Structured Programming Approach Using C 24 (continued)
Computer Science: A Structured Programming Approach Using C 25