TAILIEUCHUNG - Mastering Algorithms with Perl phần 5

phụ Sắp xếp nhanh {# phiên bản đệ quy xấu với danh sách BIG # vì ngăn xếp cuộc gọi chức năng được thực sự sâu sắc Đây là phân vùng thứ ba có thể chúng tôi gợi ý tại rộng phân vùng trung lưu càng nhiều càng tốt. + + $ i trong khi $ i | Figure 8-24. A graph and its representation in Perl Creating Graphs Dealing with Vertices First we will define functions for creating graphs and adding and checking vertices. We put these into Graph Base because later we ll see that our data structures are affected by whether or not a graph is package Graph Base use vars qw @ISA require Exporter @ISA qw Exporter new G Graph- new @V Returns a new graph G with the optional vertices @V. sub new my class shift my G bless G class G- add_vertices @_ if @_ return G Page 291 add_vertices G G- add_vertices @v - Adds the vertices to the graph G returns the graph. sub add_vertices my G @v @_ @ G- V @v @v return G add_vertex G G- add_vertex v Adds the vertex v to the graph G returns the graph. sub add_vertex my G v @_ return G- add_vertices v vertices @V G- vertices In list context returns the vertices @V of the graph G. In scalar context implicitly returns the number of the vertices. sub vertices my G shift my @V exists G- V values G- V return @V has_vertex b G- has_vertex v Returns true if the vertex v exists in the graph G and false if it doesn t. sub has_vertex my G v @_ return exists G- V - v Testing for and Adding Edges Next we ll see how to check for edges existence and how to create edges and paths. Before we tackle edges we must talk about how we treat directedness in our data structures and code. We will have a single flag per graph D that tellscontinue Page 292 whether it is of the directed or undirected kind. In addition to querying directedness we will also allow for changing it dynamically. This requires re-blessing the graph and rebuilding the set of edges. directed b G- directed d Set the directedness of the graph G to d or return the current directedness. Directedness defaults to true. sub directed my G d @_ if defined d if d my o G- D Old directedness. G- D d if not o my @E G- edges while my u v splice @E 0 2 G- add_edge v u return bless G Graph Directed Re-bless. else return G- undirected not

TỪ KHÓA LIÊN QUAN
TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.